File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616// For custom error handling
1717// TODO
1818
19+ use std:: convert:: TryFrom ;
20+
1921use crate :: collection:: Error as CollectionError ;
2022use crate :: proto:: common:: { ErrorCode , Status } ;
2123use crate :: schema:: Error as SchemaError ;
@@ -73,7 +75,13 @@ pub enum Error {
7375
7476impl From < Status > for Error {
7577 fn from ( s : Status ) -> Self {
76- Error :: Server ( ErrorCode :: from_i32 ( s. error_code ) . unwrap ( ) , s. reason )
78+ let code = ErrorCode :: try_from ( s. code ) . unwrap_or ( ErrorCode :: UnexpectedError ) ;
79+ let reason = if code == ErrorCode :: UnexpectedError && s. code != code as i32 {
80+ format ! ( "{} (code {})" , s. reason, s. code)
81+ } else {
82+ s. reason
83+ } ;
84+ Error :: Server ( code, reason)
7785 }
7886}
7987
Original file line number Diff line number Diff line change 1414// See the License for the specific language governing permissions and
1515// limitations under the License.
1616
17+ use std:: convert:: TryFrom ;
18+
1719use crate :: {
1820 error:: Error ,
1921 proto:: common:: { ErrorCode , Status } ,
@@ -24,14 +26,12 @@ pub fn status_to_result(status: &Option<Status>) -> Result<(), Error> {
2426 . clone ( )
2527 . ok_or ( Error :: Unexpected ( "no status" . to_owned ( ) ) ) ?;
2628
27- match ErrorCode :: from_i32 ( status. error_code ) {
28- Some ( i) => match i {
29- ErrorCode :: Success => Ok ( ( ) ) ,
30- _ => Err ( Error :: from ( status) ) ,
31- } ,
32- None => Err ( Error :: Unexpected ( format ! (
33- "unknown error code {}" ,
34- status. error_code
35- ) ) ) ,
29+ match ErrorCode :: try_from ( status. code ) {
30+ Ok ( ErrorCode :: Success ) => Ok ( ( ) ) ,
31+ Ok ( _) => Err ( Error :: from ( status) ) ,
32+ Err ( _) => Err ( Error :: Server (
33+ ErrorCode :: UnexpectedError ,
34+ format ! ( "{} (code {})" , status. reason, status. code) ,
35+ ) ) ,
3636 }
3737}
You can’t perform that action at this time.
0 commit comments