@@ -77,6 +77,42 @@ func (e IntentDeniedError) Error() string {
7777 return e .message
7878}
7979
80+ type SwapValidationError struct {
81+ message string
82+ }
83+
84+ func NewSwapValidationError (message string ) SwapValidationError {
85+ return SwapValidationError {
86+ message : message ,
87+ }
88+ }
89+
90+ func NewSwapValidationErrorf (format string , args ... any ) SwapValidationError {
91+ return NewSwapValidationError (fmt .Sprintf (format , args ... ))
92+ }
93+
94+ func (e SwapValidationError ) Error () string {
95+ return e .message
96+ }
97+
98+ type SwapDeniedError struct {
99+ message string
100+ }
101+
102+ func NewSwapDeniedError (message string ) SwapDeniedError {
103+ return SwapDeniedError {
104+ message : message ,
105+ }
106+ }
107+
108+ func NewSwapDeniedErrorf (format string , args ... any ) SwapDeniedError {
109+ return NewSwapDeniedError (fmt .Sprintf (format , args ... ))
110+ }
111+
112+ func (e SwapDeniedError ) Error () string {
113+ return e .message
114+ }
115+
80116type StaleStateError struct {
81117 message string
82118}
@@ -251,6 +287,61 @@ func handleSubmitIntentStructuredError(streamer transactionpb.Transaction_Submit
251287 return streamer .Send (errResp )
252288}
253289
290+ func handleSwapError (streamer transactionpb.Transaction_SwapServer , err error ) error {
291+ // gRPC status errors are passed through as is
292+ if _ , ok := status .FromError (err ); ok {
293+ return err
294+ }
295+
296+ // Case 1: Errors that map to a Code error response
297+ switch err .(type ) {
298+ case SwapValidationError :
299+ return handleSwapStructuredError (
300+ streamer ,
301+ transactionpb .SwapResponse_Error_INVALID_SWAP ,
302+ toReasonStringErrorDetails (err ),
303+ )
304+ case SwapDeniedError :
305+ return handleSwapStructuredError (
306+ streamer ,
307+ transactionpb .SwapResponse_Error_DENIED ,
308+ toDeniedErrorDetails (err ),
309+ )
310+ }
311+
312+ switch err {
313+ case ErrInvalidSignature :
314+ return handleSwapStructuredError (
315+ streamer ,
316+ transactionpb .SwapResponse_Error_SIGNATURE_ERROR ,
317+ toReasonStringErrorDetails (err ),
318+ )
319+ case ErrNotImplemented :
320+ return status .Error (codes .Unimplemented , err .Error ())
321+ }
322+
323+ // Case 2: Errors that map to gRPC status errors
324+ switch err {
325+ case ErrTimedOutReceivingRequest , context .DeadlineExceeded :
326+ return status .Error (codes .DeadlineExceeded , err .Error ())
327+ case context .Canceled :
328+ return status .Error (codes .Canceled , err .Error ())
329+ }
330+ return status .Error (codes .Internal , "rpc server failure" )
331+ }
332+
333+ func handleSwapStructuredError (streamer transactionpb.Transaction_SwapServer , code transactionpb.SwapResponse_Error_Code , errorDetails ... * transactionpb.ErrorDetails ) error {
334+ errResp := & transactionpb.SwapResponse {
335+ Response : & transactionpb.SwapResponse_Error_ {
336+ Error : & transactionpb.SwapResponse_Error {
337+ Code : code ,
338+ ErrorDetails : errorDetails ,
339+ },
340+ },
341+ }
342+ return streamer .Send (errResp )
343+ }
344+
254345func shouldFilterSubmitIntentFailureMetricReport (err error ) bool {
255346 if statusErr , ok := status .FromError (err ); ok {
256347 switch statusErr .Code () {
0 commit comments