Skip to content

Commit

Permalink
#79 better error management during Express routes handling, showing e…
Browse files Browse the repository at this point in the history
…rror when it occurs
  • Loading branch information
fcamblor committed Mar 31, 2024
1 parent bbb105d commit d9e035f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cloud/functions/src/functions/http/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export const Routes = {
const stringifiedQueryParams = stringifyParams(req.query as Record<string, string>)
exposeLogContext({ method: 'POST', route, pathParams: stringifiedPathParams, queryParams: stringifiedQueryParams }, async () => {
debug(`POST ${route} [${stringifiedPathParams}]`)
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS, req.body as BODY)
try {
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS, req.body as BODY)
} catch(e) {
return sendResponseMessage(res, 500, `Unexpected Error: ${e?.toString()}`)
}
})
})
},
Expand All @@ -106,7 +110,11 @@ export const Routes = {
const stringifiedQueryParams = stringifyParams(req.query as Record<string, string>)
exposeLogContext({ method: 'PUT', route, pathParams: stringifiedPathParams, queryParams: stringifiedQueryParams }, async () => {
debug(`PUT ${route} [${stringifiedPathParams}]`)
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS, req.body as BODY);
try {
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS, req.body as BODY)
} catch(e) {
return sendResponseMessage(res, 500, `Unexpected Error: ${e?.toString()}`)
}
})
})
},
Expand All @@ -121,7 +129,11 @@ export const Routes = {
const stringifiedQueryParams = stringifyParams(req.query as Record<string, string>)
exposeLogContext({ method: 'GET', route, pathParams: stringifiedPathParams, queryParams: stringifiedQueryParams }, async () => {
debug(`GET ${route} [${stringifiedPathParams}]`)
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS);
try {
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS);
} catch(e) {
return sendResponseMessage(res, 500, `Unexpected Error: ${e?.toString()}`)
}
})
})
},
Expand All @@ -136,7 +148,11 @@ export const Routes = {
const stringifiedQueryParams = stringifyParams(req.query as Record<string, string>)
exposeLogContext({ method: 'DELETE', route, pathParams: stringifiedPathParams, queryParams: stringifiedQueryParams }, async () => {
debug(`DELETE ${route} [${stringifiedPathParams}]`)
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS)
try {
await callback(res, req.params as PATH_PARAMS, req.query as QUERY_PARAMS);
} catch(e) {
return sendResponseMessage(res, 500, `Unexpected Error: ${e?.toString()}`)
}
})
})
}
Expand Down

0 comments on commit d9e035f

Please sign in to comment.