mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 03:27:38 +00:00
fix: error improvements
This commit is contained in:
@@ -19,28 +19,47 @@ enum JWTErrors {
|
|||||||
InvalidAlgorithm = "invalid algorithm"
|
InvalidAlgorithm = "invalid algorithm"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum HttpStatusCodes {
|
||||||
|
BadRequest = 400,
|
||||||
|
NotFound = 404,
|
||||||
|
Unauthorized = 401,
|
||||||
|
Forbidden = 403,
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||||
|
InternalServerError = 500
|
||||||
|
}
|
||||||
|
|
||||||
export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider) => {
|
export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider) => {
|
||||||
server.setErrorHandler((error, req, res) => {
|
server.setErrorHandler((error, req, res) => {
|
||||||
req.log.error(error);
|
req.log.error(error);
|
||||||
if (error instanceof BadRequestError) {
|
if (error instanceof BadRequestError) {
|
||||||
void res.status(400).send({ statusCode: 400, message: error.message, error: error.name });
|
void res
|
||||||
|
.status(HttpStatusCodes.BadRequest)
|
||||||
|
.send({ statusCode: HttpStatusCodes.BadRequest, message: error.message, error: error.name });
|
||||||
} else if (error instanceof NotFoundError) {
|
} else if (error instanceof NotFoundError) {
|
||||||
void res.status(404).send({ statusCode: 404, message: error.message, error: error.name });
|
void res
|
||||||
|
.status(HttpStatusCodes.NotFound)
|
||||||
|
.send({ statusCode: HttpStatusCodes.NotFound, message: error.message, error: error.name });
|
||||||
} else if (error instanceof UnauthorizedError) {
|
} else if (error instanceof UnauthorizedError) {
|
||||||
void res.status(401).send({ statusCode: 401, message: error.message, error: error.name });
|
void res
|
||||||
|
.status(HttpStatusCodes.Unauthorized)
|
||||||
|
.send({ statusCode: HttpStatusCodes.Unauthorized, message: error.message, error: error.name });
|
||||||
} else if (error instanceof DatabaseError || error instanceof InternalServerError) {
|
} else if (error instanceof DatabaseError || error instanceof InternalServerError) {
|
||||||
void res.status(500).send({ statusCode: 500, message: "Something went wrong", error: error.name });
|
void res
|
||||||
|
.status(HttpStatusCodes.InternalServerError)
|
||||||
|
.send({ statusCode: HttpStatusCodes.InternalServerError, message: "Something went wrong", error: error.name });
|
||||||
} else if (error instanceof ZodError) {
|
} else if (error instanceof ZodError) {
|
||||||
void res.status(401).send({ statusCode: 401, error: "ValidationFailure", message: error.issues });
|
void res
|
||||||
|
.status(HttpStatusCodes.Unauthorized)
|
||||||
|
.send({ statusCode: HttpStatusCodes.Unauthorized, error: "ValidationFailure", message: error.issues });
|
||||||
} else if (error instanceof ForbiddenError) {
|
} else if (error instanceof ForbiddenError) {
|
||||||
void res.status(403).send({
|
void res.status(HttpStatusCodes.Forbidden).send({
|
||||||
statusCode: 403,
|
statusCode: HttpStatusCodes.Forbidden,
|
||||||
error: "PermissionDenied",
|
error: "PermissionDenied",
|
||||||
message: `You are not allowed to ${error.action} on ${error.subjectType}`
|
message: `You are not allowed to ${error.action} on ${error.subjectType}`
|
||||||
});
|
});
|
||||||
} else if (error instanceof ForbiddenRequestError) {
|
} else if (error instanceof ForbiddenRequestError) {
|
||||||
void res.status(403).send({
|
void res.status(HttpStatusCodes.Forbidden).send({
|
||||||
statusCode: 403,
|
statusCode: HttpStatusCodes.Forbidden,
|
||||||
message: error.message,
|
message: error.message,
|
||||||
error: error.name
|
error: error.name
|
||||||
});
|
});
|
||||||
@@ -66,8 +85,8 @@ export const fastifyErrHandler = fastifyPlugin(async (server: FastifyZodProvider
|
|||||||
return error.message;
|
return error.message;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
void res.status(403).send({
|
void res.status(HttpStatusCodes.Forbidden).send({
|
||||||
statusCode: 403,
|
statusCode: HttpStatusCodes.Forbidden,
|
||||||
error: "TokenError",
|
error: "TokenError",
|
||||||
message
|
message
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -40,12 +40,12 @@ export const DefaultResponseErrorsSchema = {
|
|||||||
}),
|
}),
|
||||||
401: z.object({
|
401: z.object({
|
||||||
statusCode: z.literal(401),
|
statusCode: z.literal(401),
|
||||||
message: z.string(),
|
message: z.any(),
|
||||||
error: z.string()
|
error: z.string()
|
||||||
}),
|
}),
|
||||||
403: z.object({
|
403: z.object({
|
||||||
statusCode: z.literal(403),
|
statusCode: z.literal(403),
|
||||||
message: z.any(),
|
message: z.string(),
|
||||||
error: z.string()
|
error: z.string()
|
||||||
}),
|
}),
|
||||||
500: z.object({
|
500: z.object({
|
||||||
|
|||||||
Reference in New Issue
Block a user