MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / createAPIError

Function createAPIError

frontend/src/static/js/api/errors.ts:19–55  ·  view source on GitHub ↗
(
  error?: {} | null | undefined,
  statusCode?: number,
)

Source from the content-addressed store, hash-verified

17 */
18
19export function createAPIError(
20 error?: {} | null | undefined,
21 statusCode?: number,
22): ApiError {
23 let message = 'Unknown error';
24 let code = statusCode || 500; // Use provided status code as default
25
26 if (
27 error instanceof Object &&
28 'message' in error &&
29 typeof error.message === 'string'
30 ) {
31 message = error.message;
32 if (
33 'errors' in error &&
34 error.errors instanceof Object &&
35 error.errors !== null
36 ) {
37 const entries = Object.entries(error.errors);
38 const fieldMsgs = entries
39 .filter(([, msg]) => typeof msg === 'string')
40 .map(([field, msg]) => `${field}: ${msg}`)
41 .join(', ');
42 if (fieldMsgs) {
43 message = fieldMsgs;
44 }
45 }
46 if ('code' in error && typeof error.code === 'number') {
47 code = error.code;
48 }
49 } else if (error instanceof Error) {
50 message = error.message;
51 }
52
53 switch (code) {
54 case 400:
55 return new BadRequestError(message);
56 case 401:
57 return new UnauthorizedError(message);
58 case 403:

Callers 2

getPageOfDataMethod · 0.90
handleResponseMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected