MCPcopy Create free account
hub / github.com/boringstack-xyz/boringstack / errorHandler

Function errorHandler

apps/api/src/middleware/error-handler.ts:104–171  ·  view source on GitHub ↗
({
  code,
  error,
  set,
}: IErrorHandlerArgs)

Source from the content-addressed store, hash-verified

102 return 404;
103 case ElysiaErrorCodes.VALIDATION:
104 case ElysiaErrorCodes.PARSE:
105 return 400;
106 case ElysiaErrorCodes.INVALID_COOKIE_SIGNATURE:
107 return 401;
108 default:
109 return 500;
110 }
111};
112
113export const errorHandler = ({
114 code,
115 error,
116 set,
117}: IErrorHandlerArgs): IApiErrorResponse => {
118 const isClientError = isClientErrorCode(code) || isClientApiError(error);
119 const finalStatus = resolveFinalStatus(code, error);
120
121 const basePayload = {
122 errorCode: code,
123 statusCode: finalStatus,
124 message:
125 error instanceof ApiError ? error.message : safeMessageFor(code, error),
126 stack: stackFor(code, error, env.isDevelopment),
127 };
128
129 if (isClientError) {
130 logger.warn(`Client error ${code}`, {
131 ...basePayload,
132 event: "request.client_error",
133 });
134 } else {
135 logger.error(`Error ${code}`, {
136 ...basePayload,
137 event: "request.error",
138 });
139 }
140
141 if (error instanceof ApiError) {
142 set.status = finalStatus;
143
144 return error.toResponse();
145 }
146
147 set.status = finalStatus;
148
149 let apiError: ApiError;
150
151 switch (code) {
152 case ElysiaErrorCodes.NOT_FOUND:
153 apiError = ApiErrors.notFound();
154 break;
155 case ElysiaErrorCodes.VALIDATION:
156 case ElysiaErrorCodes.PARSE:
157 apiError = ApiErrors.validation(
158 safeMessageFor(code, error),
159 extractFieldErrors(error)
160 );
161 break;

Callers 15

users.routes.tsFile · 0.90
mfa.routes.tsFile · 0.90
auth.routes.tsFile · 0.90
accounts.routes.tsFile · 0.90
billing.routes.tsFile · 0.90
sendgrid.routes.tsFile · 0.90
resend.routes.tsFile · 0.90
admin.routes.tsFile · 0.90

Calls 8

isClientErrorCodeFunction · 0.85
isClientApiErrorFunction · 0.85
resolveFinalStatusFunction · 0.85
safeMessageForFunction · 0.85
extractFieldErrorsFunction · 0.85
warnMethod · 0.80
errorMethod · 0.80
toResponseMethod · 0.80

Tested by 5

buildAppFunction · 0.72
buildAppFunction · 0.72
buildAppFunction · 0.72
buildProbeAppFunction · 0.72
buildFreshProbeAppFunction · 0.72