-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathserviceError.ts
More file actions
161 lines (141 loc) · 4.67 KB
/
serviceError.ts
File metadata and controls
161 lines (141 loc) · 4.67 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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 invalidZoektResponse = async (zoektResponse: Response): Promise<ServiceError> => {
const zoektMessage = await (async () => {
try {
const zoektResponseBody = await zoektResponse.json();
if (zoektResponseBody.Error) {
return zoektResponseBody.Error;
}
} catch (_e) {
return "Unknown error";
}
})();
return {
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
errorCode: ErrorCode.INVALID_REQUEST_BODY,
message: `Zoekt request failed with status code ${zoektResponse.status} and message: "${zoektMessage}"`,
};
}
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 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 orgDomainExists = (): ServiceError => {
return {
statusCode: StatusCodes.CONFLICT,
errorCode: ErrorCode.ORG_DOMAIN_ALREADY_EXISTS,
message: "Organization domain already exists, please try a different one.",
}
}
export const orgInvalidSubscription = (): ServiceError => {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.ORG_INVALID_SUBSCRIPTION,
message: "Invalid subscription",
}
}
export const secretAlreadyExists = (): ServiceError => {
return {
statusCode: StatusCodes.CONFLICT,
errorCode: ErrorCode.SECRET_ALREADY_EXISTS,
message: "Secret already exists",
}
}
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 stripeClientNotInitialized = (): ServiceError => {
return {
statusCode: StatusCodes.INTERNAL_SERVER_ERROR,
errorCode: ErrorCode.STRIPE_CLIENT_NOT_INITIALIZED,
message: "Stripe client is not initialized.",
}
}