@@ -18,8 +18,10 @@ use std::fmt;
1818
1919use ccore:: AccountProviderError ;
2020use ccore:: Error as CoreError ;
21+ use ckey:: Error as KeyError ;
2122use cnetwork:: control:: Error as NetworkControlError ;
2223use cstate:: StateError ;
24+ use ctypes:: parcel:: Error as ParcelError ;
2325use kvdb:: Error as KVDBError ;
2426use rlp:: DecoderError ;
2527
@@ -28,13 +30,20 @@ use jsonrpc_core::{Error, ErrorCode, Value};
2830mod codes {
2931 pub const NO_AUTHOR : i64 = -32002 ;
3032 pub const NO_WORK_REQUIRED : i64 = -32004 ;
31- pub const UNKNOWN_ERROR : i64 = -32009 ;
33+ pub const RLP_ERROR : i64 = -32009 ;
3234 pub const CORE_ERROR : i64 = -32010 ;
3335 pub const KVDB_ERROR : i64 = -32011 ;
3436 pub const PARCEL_ERROR : i64 = -32012 ;
3537 pub const NETWORK_DISABLED : i64 = -32014 ;
3638 pub const NETWORK_CANNOT_DISCONNECT_NOT_CONNECTED_ERROR : i64 = -32015 ;
3739 pub const ACCOUNT_PROVIDER_ERROR : i64 = -32016 ;
40+ pub const VERIFICATION_FAILED : i64 = -32030 ;
41+ pub const ALREADY_IMPORTED : i64 = -32031 ;
42+ pub const NOT_ENOUGH_BALANCE : i64 = -32032 ;
43+ pub const TOO_LOW_FEE : i64 = -32033 ;
44+ pub const TOO_CHEAP_TO_REPLACE : i64 = -32034 ;
45+ pub const INVALID_NONCE : i64 = -32035 ;
46+ pub const UNKNOWN_ERROR : i64 = -32099 ;
3847}
3948
4049pub fn core < T : Into < CoreError > > ( error : T ) -> Error {
@@ -65,18 +74,60 @@ pub fn parcel_state<T: Into<StateError>>(error: T) -> Error {
6574
6675pub fn parcel_core < T : Into < CoreError > > ( error : T ) -> Error {
6776 let error = error. into ( ) ;
68- if let CoreError :: State ( StateError :: Parcel ( e) ) = error {
69- Error {
70- code : ErrorCode :: ServerError ( codes:: PARCEL_ERROR ) ,
71- message : format ! ( "{}" , e) ,
72- data : None ,
73- }
74- } else {
75- Error {
76- code : ErrorCode :: ServerError ( codes:: UNKNOWN_ERROR ) ,
77- message : "Unknown error when sending parcel." . into ( ) ,
78- data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
79- }
77+ let unknown_error = Error {
78+ code : ErrorCode :: ServerError ( codes:: UNKNOWN_ERROR ) ,
79+ message : "Unknown error when sending parcel." . into ( ) ,
80+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
81+ } ;
82+ match error {
83+ CoreError :: Key ( error) => match error {
84+ KeyError :: InvalidSignature => Error {
85+ code : ErrorCode :: ServerError ( codes:: VERIFICATION_FAILED ) ,
86+ message : "Verification Failed" . into ( ) ,
87+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
88+ } ,
89+ _ => unknown_error,
90+ } ,
91+ CoreError :: State ( StateError :: Parcel ( error) ) => match error {
92+ ParcelError :: InvalidSignature ( _) | ParcelError :: InvalidNetworkId => Error {
93+ code : ErrorCode :: ServerError ( codes:: VERIFICATION_FAILED ) ,
94+ message : "Verification Failed" . into ( ) ,
95+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
96+ } ,
97+ ParcelError :: ParcelAlreadyImported => Error {
98+ code : ErrorCode :: ServerError ( codes:: ALREADY_IMPORTED ) ,
99+ message : "Already Imported" . into ( ) ,
100+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
101+ } ,
102+ ParcelError :: InsufficientBalance {
103+ ..
104+ } => Error {
105+ code : ErrorCode :: ServerError ( codes:: NOT_ENOUGH_BALANCE ) ,
106+ message : "Not Enough Balance" . into ( ) ,
107+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
108+ } ,
109+ ParcelError :: InsufficientFee {
110+ ..
111+ } => Error {
112+ code : ErrorCode :: ServerError ( codes:: TOO_LOW_FEE ) ,
113+ message : "Too Low Fee" . into ( ) ,
114+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
115+ } ,
116+ ParcelError :: TooCheapToReplace => Error {
117+ code : ErrorCode :: ServerError ( codes:: TOO_CHEAP_TO_REPLACE ) ,
118+ message : "Too Cheap to Replace" . into ( ) ,
119+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
120+ } ,
121+ ParcelError :: Old {
122+ ..
123+ } => Error {
124+ code : ErrorCode :: ServerError ( codes:: INVALID_NONCE ) ,
125+ message : "Invalid Nonce" . into ( ) ,
126+ data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
127+ } ,
128+ _ => unknown_error,
129+ } ,
130+ _ => unknown_error,
80131 }
81132}
82133
@@ -90,7 +141,7 @@ pub fn kvdb(error: KVDBError) -> Error {
90141
91142pub fn rlp ( error : DecoderError ) -> Error {
92143 Error {
93- code : ErrorCode :: ServerError ( codes:: UNKNOWN_ERROR ) ,
144+ code : ErrorCode :: ServerError ( codes:: RLP_ERROR ) ,
94145 message : "Invalid RLP." . into ( ) ,
95146 data : Some ( Value :: String ( format ! ( "{:?}" , error) ) ) ,
96147 }
0 commit comments