@@ -7,7 +7,7 @@ use endpoint_libs::libs::handler::RequestHandler;
77use endpoint_libs:: libs:: log:: error_aggregation:: ErrorAggregationConfig ;
88use endpoint_libs:: libs:: log:: { LogLevel , LoggingConfig , OtelConfig , setup_logging} ;
99use endpoint_libs:: libs:: toolbox:: { ArcToolbox , RequestContext } ;
10- use endpoint_libs:: libs:: ws:: toolbox:: CustomError ;
10+ use endpoint_libs:: libs:: ws:: toolbox:: { CustomError , HandlerError } ;
1111use endpoint_libs:: libs:: ws:: {
1212 AuthController , WebsocketServer , WsConnection , WsRequest , WsResponse , WsServerConfig ,
1313} ;
@@ -88,20 +88,81 @@ impl WsResponse for HoneyReceiveUserInfoResponse {
8888 type Request = HoneyReceiveUserInfoRequest ;
8989}
9090
91+ // --- Typed error enum example ---
92+ //
93+ // This demonstrates the HandlerError trait for defining per-handler error codes.
94+
95+ #[ derive( Debug , Clone ) ]
96+ pub enum EchoTypedError {
97+ MessageTooLong { max_length : u32 , actual_length : u32 } ,
98+ EmptyMessage ,
99+ }
100+
101+ impl std:: fmt:: Display for EchoTypedError {
102+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
103+ match self {
104+ Self :: MessageTooLong { max_length, actual_length } => {
105+ write ! ( f, "message length {} exceeds maximum {}" , actual_length, max_length)
106+ }
107+ Self :: EmptyMessage => write ! ( f, "message cannot be empty" ) ,
108+ }
109+ }
110+ }
111+
112+ impl std:: error:: Error for EchoTypedError { }
113+
114+ impl HandlerError for EchoTypedError {
115+ fn error_code ( & self ) -> ErrorCode {
116+ match self {
117+ // Handler-specific codes for Echo (method 1): 1001-1099
118+ Self :: MessageTooLong { .. } => ErrorCode :: new ( 1001 ) ,
119+ Self :: EmptyMessage => ErrorCode :: new ( 1002 ) ,
120+ }
121+ }
122+
123+ fn params ( & self ) -> String {
124+ match self {
125+ Self :: MessageTooLong { max_length, actual_length } => {
126+ format ! ( "message length {} exceeds maximum {}" , actual_length, max_length)
127+ }
128+ Self :: EmptyMessage => "empty message not allowed" . to_string ( ) ,
129+ }
130+ }
131+ }
132+
133+ impl From < EchoTypedError > for CustomError {
134+ fn from ( err : EchoTypedError ) -> Self {
135+ CustomError :: new ( err. error_code ( ) , err. params ( ) )
136+ }
137+ }
138+
91139// --- Handlers ---
92140
93141pub struct MethodEcho ;
94142
95143#[ async_trait( ?Send ) ]
96144impl RequestHandler for MethodEcho {
97145 type Request = EchoRequest ;
146+ type Error = EchoTypedError ;
98147
99- async fn handle ( & self , ctx : RequestContext , req : EchoRequest ) -> Result < EchoResponse > {
148+ async fn handle ( & self , ctx : RequestContext , req : EchoRequest ) -> Result < EchoResponse , EchoTypedError > {
100149 tracing:: info!(
101150 conn_id = %ctx. connection_id,
102151 message = %req. message,
103152 "Echo request received"
104153 ) ;
154+
155+ if req. message . is_empty ( ) {
156+ return Err ( EchoTypedError :: EmptyMessage ) ;
157+ }
158+
159+ if req. message . len ( ) > 1000 {
160+ return Err ( EchoTypedError :: MessageTooLong {
161+ max_length : 1000 ,
162+ actual_length : req. message . len ( ) as u32 ,
163+ } ) ;
164+ }
165+
105166 let response = EchoResponse {
106167 message : format ! ( "echo: {}" , req. message) ,
107168 } ;
@@ -119,12 +180,13 @@ pub struct MethodReceiveUserInfo;
119180#[ async_trait( ?Send ) ]
120181impl RequestHandler for MethodReceiveUserInfo {
121182 type Request = HoneyReceiveUserInfoRequest ;
183+ type Error = CustomError ;
122184
123185 async fn handle (
124186 & self ,
125187 ctx : RequestContext ,
126188 req : HoneyReceiveUserInfoRequest ,
127- ) -> Result < HoneyReceiveUserInfoResponse > {
189+ ) -> Result < HoneyReceiveUserInfoResponse , CustomError > {
128190 tracing:: info!(
129191 conn_id = %ctx. connection_id,
130192 user_pub_id = %req. user_pub_id,
@@ -147,7 +209,7 @@ impl RequestHandler for MethodReceiveUserInfo {
147209 user_pub_id = %req. user_pub_id,
148210 "Rejecting ReceiveUserInfo with BAD_REQUEST"
149211 ) ;
150- Err ( CustomError :: new ( ErrorCode :: BAD_REQUEST , msg) . into ( ) )
212+ Err ( CustomError :: new ( ErrorCode :: BAD_REQUEST , msg) )
151213 }
152214}
153215
0 commit comments