|
1 | 1 | import { Request, Response as ExpressResponse, NextFunction } from 'express'; |
2 | 2 | import { Config } from '../types'; |
3 | 3 | import { BitGoRequest } from '../types/request'; |
| 4 | +import { EnclavedError } from '../errors'; |
4 | 5 |
|
5 | 6 | // Extend Express Response to include sendEncoded |
6 | 7 | interface EncodedResponse extends ExpressResponse { |
@@ -31,19 +32,26 @@ export function responseHandler<T extends Config = Config>(fn: ServiceFunction<T |
31 | 32 | const result = await fn(req as BitGoRequest<T>, res, next); |
32 | 33 | return res.sendEncoded(result.type, result.payload); |
33 | 34 | } catch (error) { |
34 | | - // Log the error |
35 | | - console.error('Error in service function:', error); |
36 | | - |
37 | 35 | // If it's already a Response object (e.g. from Response.error) |
38 | 36 | if (error && typeof error === 'object' && 'type' in error && 'payload' in error) { |
39 | 37 | const apiError = error as ApiResponse; |
40 | 38 | return res.sendEncoded(apiError.type, apiError.payload); |
41 | 39 | } |
42 | 40 |
|
| 41 | + // If it's an EnclavedError, use its status code |
| 42 | + if (error instanceof EnclavedError) { |
| 43 | + return res.sendEncoded(error.status, { |
| 44 | + error: error.message, |
| 45 | + name: error.name, |
| 46 | + details: error.message, |
| 47 | + }); |
| 48 | + } |
| 49 | + |
43 | 50 | // Default error response |
44 | 51 | return res.sendEncoded(500, { |
45 | 52 | error: 'Internal Server Error', |
46 | | - message: error instanceof Error ? error.message : 'An unexpected error occurred', |
| 53 | + name: error instanceof Error ? error.name : 'Error', |
| 54 | + details: error instanceof Error ? error.message : String(error), |
47 | 55 | }); |
48 | 56 | } |
49 | 57 | }; |
|
0 commit comments