simplify
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
/**
|
||||
* API Error Handler
|
||||
*
|
||||
*
|
||||
* Centralizes error handling for API endpoints by converting
|
||||
* application errors into appropriate HTTP responses.
|
||||
*
|
||||
*
|
||||
* Maps error types to status codes:
|
||||
* - ValidationError → 400 Bad Request
|
||||
* - NotFoundError → 404 Not Found
|
||||
* - ConflictError → 409 Conflict
|
||||
* - Other errors → 500 Internal Server Error
|
||||
*
|
||||
*
|
||||
* Provides consistent error response format across all API endpoints.
|
||||
*/
|
||||
|
||||
@@ -19,46 +19,56 @@ import { logError } from '../utils/logger';
|
||||
|
||||
/**
|
||||
* Handle API errors and convert to appropriate HTTP responses
|
||||
*
|
||||
*
|
||||
* @param error - Error to handle (can be any type)
|
||||
* @returns JSON response with appropriate status code and error message
|
||||
*/
|
||||
export function handleApiError(error: unknown): Response {
|
||||
// Log all errors for debugging
|
||||
logError('[API Error]', error);
|
||||
// Log all errors for debugging
|
||||
logError('[API Error]', error);
|
||||
|
||||
// Handle known error types with specific status codes
|
||||
if (error instanceof ValidationError) {
|
||||
return json({
|
||||
message: error.message,
|
||||
type: 'validation_error'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
if (error instanceof NotFoundError) {
|
||||
return json({
|
||||
message: error.message,
|
||||
type: 'not_found_error'
|
||||
}, { status: 404 });
|
||||
}
|
||||
|
||||
if (error instanceof ConflictError) {
|
||||
return json({
|
||||
message: error.message,
|
||||
type: 'conflict_error'
|
||||
}, { status: 409 });
|
||||
}
|
||||
// Handle known error types with specific status codes
|
||||
if (error instanceof ValidationError) {
|
||||
return json(
|
||||
{
|
||||
message: error.message,
|
||||
type: 'validation_error'
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Handle generic errors
|
||||
const message = error instanceof Error ? error.message : 'Unknown error occurred';
|
||||
|
||||
// Don't expose internal error details in production
|
||||
const publicMessage = process.env.NODE_ENV === 'production'
|
||||
? 'Internal server error'
|
||||
: message;
|
||||
if (error instanceof NotFoundError) {
|
||||
return json(
|
||||
{
|
||||
message: error.message,
|
||||
type: 'not_found_error'
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return json({
|
||||
message: publicMessage,
|
||||
type: 'server_error'
|
||||
}, { status: 500 });
|
||||
}
|
||||
if (error instanceof ConflictError) {
|
||||
return json(
|
||||
{
|
||||
message: error.message,
|
||||
type: 'conflict_error'
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Handle generic errors
|
||||
const message = error instanceof Error ? error.message : 'Unknown error occurred';
|
||||
|
||||
// Don't expose internal error details in production
|
||||
const publicMessage = process.env.NODE_ENV === 'production' ? 'Internal server error' : message;
|
||||
|
||||
return json(
|
||||
{
|
||||
message: publicMessage,
|
||||
type: 'server_error'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
/**
|
||||
* Custom Error Classes for API Error Handling
|
||||
*
|
||||
*
|
||||
* Defines specific error types that map to HTTP status codes:
|
||||
* - ValidationError → 400 Bad Request
|
||||
* - NotFoundError → 404 Not Found
|
||||
* - NotFoundError → 404 Not Found
|
||||
* - ConflictError → 409 Conflict
|
||||
*
|
||||
*
|
||||
* Used by API endpoints to throw meaningful errors that are
|
||||
* caught and converted to proper HTTP responses by errorHandler.ts
|
||||
*/
|
||||
@@ -15,10 +15,10 @@
|
||||
* Thrown when request data is invalid or malformed
|
||||
*/
|
||||
export class ValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ValidationError';
|
||||
}
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ValidationError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,10 +26,10 @@ export class ValidationError extends Error {
|
||||
* Thrown when requested resource does not exist
|
||||
*/
|
||||
export class NotFoundError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'NotFoundError';
|
||||
}
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'NotFoundError';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,8 +37,8 @@ export class NotFoundError extends Error {
|
||||
* Thrown when operation conflicts with current resource state
|
||||
*/
|
||||
export class ConflictError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ConflictError';
|
||||
}
|
||||
}
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'ConflictError';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user