auth-service / src /utils /apiError.ts
seraj-b2
Initial commit of Google Auth Backend Service
b480ef7
Raw History Blame Contribute Delete
1.38 kB
export class ApiError extends Error {
public readonly statusCode: number;
public readonly isOperational: boolean;
public readonly details?: unknown;
constructor(statusCode: number, message: string, details?: unknown, isOperational = true) {
super(message);
this.statusCode = statusCode;
this.isOperational = isOperational;
this.details = details;
Object.setPrototypeOf(this, new.target.prototype);
Error.captureStackTrace(this, this.constructor);
}
static badRequest(message: string, details?: unknown): ApiError {
return new ApiError(400, message, details);
}
static unauthorized(message = 'Unauthorized', details?: unknown): ApiError {
return new ApiError(401, message, details);
}
static forbidden(message = 'Forbidden', details?: unknown): ApiError {
return new ApiError(403, message, details);
}
static notFound(message = 'Resource not found', details?: unknown): ApiError {
return new ApiError(404, message, details);
}
static conflict(message: string, details?: unknown): ApiError {
return new ApiError(409, message, details);
}
static tooManyRequests(message = 'Too many requests, please try again later.'): ApiError {
return new ApiError(429, message);
}
static internal(message = 'Internal server error'): ApiError {
return new ApiError(500, message, undefined, false);
}
}