-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathserviceError.ts
More file actions
129 lines (112 loc) · 3.82 KB
/
Copy pathserviceError.ts
File metadata and controls
129 lines (112 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { StatusCodes } from "http-status-codes";
import { ErrorCode } from "./errorCodes";
import { z, ZodError } from "zod";
export const serviceErrorSchema = z.object({
statusCode: z.number(),
errorCode: z.string(),
message: z.string(),
});
export type ServiceError = z.infer<typeof serviceErrorSchema>;
/**
* Useful for throwing errors and handling them in error boundaries.
*/
export class ServiceErrorException extends Error {
constructor(public readonly serviceError: ServiceError) {
super(JSON.stringify(serviceError));
}
}
export const serviceErrorResponse = ({ statusCode, errorCode, message }: ServiceError) => {
return Response.json({
statusCode,
errorCode,
message,
}, {
status: statusCode,
});
}
export const missingQueryParam = (name: string): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.MISSING_REQUIRED_QUERY_PARAMETER,
message: `Missing required query parameter: ${name}`,
};
}
export const requestBodySchemaValidationError = (error: ZodError): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_REQUEST_BODY,
message: `Schema validation failed with: ${error.message}`,
};
}
export const queryParamsSchemaValidationError = (error: ZodError): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_QUERY_PARAMS,
message: `Query params validation failed with: ${error.message}`,
};
}
export const fileNotFound = (fileName: string, repository: string): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.FILE_NOT_FOUND,
message: `File "${fileName}" not found in repository "${repository}"`,
};
}
export const unexpectedError = (message: string): ServiceError => {
return {
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
errorCode: ErrorCode.UNEXPECTED_ERROR,
message: `Unexpected error: ${message}`,
};
}
export const lighthouseUnreachable = (url: string, error: unknown): ServiceError => {
const detail = error instanceof Error ? error.message : String(error);
return {
statusCode: StatusCodes.SERVICE_UNAVAILABLE,
errorCode: ErrorCode.LIGHTHOUSE_UNREACHABLE,
message: `Could not reach the Sourcebot licensing service at ${url}. `
+ `Verify this host has outbound network access to it, then try again. Details: ${detail}`,
};
}
export const notAuthenticated = (): ServiceError => {
return {
statusCode: StatusCodes.UNAUTHORIZED,
errorCode: ErrorCode.NOT_AUTHENTICATED,
message: "Not authenticated",
}
}
export const notFound = (message?: string): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.NOT_FOUND,
message: message ?? "Not found",
}
}
export const userNotFound = (): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.USER_NOT_FOUND,
message: "User not found",
}
}
export const orgNotFound = (): ServiceError => {
return {
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.ORG_NOT_FOUND,
message: "Organization not found",
}
}
export const invalidGitRef = (ref: string): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_GIT_REF,
message: `Invalid git reference: "${ref}". Git refs cannot start with '-'.`,
};
}
export const unresolvedGitRef = (ref: string): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_GIT_REF,
message: `Git reference "${ref}" could not be resolved.`,
};
}