Files
HIPCTF2/backend/src/common/errors/api-error.ts
T

37 lines
1.2 KiB
TypeScript

import { HttpException, HttpStatus } from '@nestjs/common';
import { ErrorCode, ERROR_CODES } from './error-codes';
export class ApiError extends HttpException {
constructor(
public readonly code: ErrorCode,
message: string,
status: HttpStatus,
public readonly details?: unknown,
) {
super({ code, message, details }, status);
}
static validation(message = 'Validation failed', details?: unknown): ApiError {
return new ApiError(ERROR_CODES.VALIDATION_FAILED, message, HttpStatus.BAD_REQUEST, details);
}
static unauthorized(message = 'Unauthorized'): ApiError {
return new ApiError(ERROR_CODES.UNAUTHORIZED, message, HttpStatus.UNAUTHORIZED);
}
static forbidden(message = 'Forbidden'): ApiError {
return new ApiError(ERROR_CODES.FORBIDDEN, message, HttpStatus.FORBIDDEN);
}
static notFound(message = 'Not found'): ApiError {
return new ApiError(ERROR_CODES.NOT_FOUND, message, HttpStatus.NOT_FOUND);
}
static conflict(code: ErrorCode, message: string): ApiError {
return new ApiError(code, message, HttpStatus.CONFLICT);
}
static internal(message = 'Internal server error'): ApiError {
return new ApiError(ERROR_CODES.INTERNAL, message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}