-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapi-error.ts
More file actions
69 lines (60 loc) · 2.13 KB
/
Copy pathapi-error.ts
File metadata and controls
69 lines (60 loc) · 2.13 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
import { NextRequest, NextResponse } from 'next/server';
/**
* A standardized error response format for the API.
* @param message The error message.
* @param status The HTTP status code.
* @returns A NextResponse object with a standardized error body.
*/
export function apiErrorResponse(message: string, status: number) {
return NextResponse.json({ error: message }, { status });
}
/**
* Base class for custom API errors.
* Allows bundling a message and an HTTP status code together.
*/
export class ApiError extends Error {
public readonly status: number;
constructor(message: string, status: number) {
super(message);
this.status = status;
}
}
export class NotFoundError extends ApiError {
constructor(message: string = 'Resource not found') {
super(message, 404);
}
}
export class BadRequestError extends ApiError {
constructor(message: string = 'Bad request') {
super(message, 400);
}
}
export class UnauthorizedError extends ApiError {
constructor(message: string = 'Unauthorized') {
super(message, 401);
}
}
// A type for API handlers to ensure consistency.
// Note: 'props' is 'any' to accommodate different route parameter structures.
type ApiHandler = (req: NextRequest, props: any) => Promise<NextResponse>;
/**
* A higher-order function to wrap API route handlers with standardized error handling.
* @param handler The API route handler function to wrap.
* @returns A new handler function with built-in try-catch logic.
*/
export function withErrorHandler(handler: ApiHandler): ApiHandler {
return async (req: NextRequest, props: any) => {
try {
return await handler(req, props);
} catch (error) {
if (error instanceof ApiError) {
return apiErrorResponse(error.message, error.status);
}
// For unknown or unexpected errors, log them for debugging purposes.
// In a real production environment, you would use a dedicated logging service.
console.error('An unexpected error occurred:', error);
// Return a generic 500 response to avoid leaking implementation details.
return apiErrorResponse('An internal server error occurred', 500);
}
};
}