You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A fork from [next-safe-route](https://github.com/richardsolomou/next-safe-route) that uses [zod](https://github.com/colinhacks/zod)instead of [typeschema](https://github.com/typeschema/main).
3
+
A fork from [next-safe-route](https://github.com/richardsolomou/next-safe-route) that uses [zod](https://github.com/colinhacks/zod)for schema validation.
2.**Return a plain object** that will be automatically converted to a JSON response with status 200:
113
+
114
+
```ts
115
+
return { data: 'value' };
116
+
```
117
+
69
118
## Advanced Usage
70
119
71
120
### Middleware
72
121
73
-
You can add middleware to your route handler with the `use` method.
122
+
You can add middleware to your route handler with the `use` method. Middleware functions can add data to the context that will be available in your handler.
returnNextResponse.json({ message: 'Something went wrong' }, { status: 400 });
178
+
// Default error response
179
+
returnnewResponse(JSON.stringify({ message: 'Internal server error' }), { status: 500 });
115
180
},
116
181
});
117
-
```
118
182
119
-
- Use the `handleServerError` method to define a custom error handler.:
120
-
121
-
```ts
122
-
const GET =safeRoute.handler((request, context) => {
123
-
// This error will be handled by the custom error handler with a 500 status code
124
-
thrownewRouteError('Test error', 500);
183
+
exportconst GET =safeRoute.handler((request, context) => {
184
+
// This error will be caught by the custom error handler
185
+
thrownewCustomError('Something went wrong', 400);
125
186
});
126
187
```
127
188
128
-
By default, to avoid any information leakage, the error handler will always return a generic error message.
189
+
By default, if no custom error handler is provided, the library will return a generic "Internal server error" message with a 500 status code to avoid information leakage.
190
+
191
+
## Validation Errors
192
+
193
+
When validation fails, the library returns appropriate error responses:
194
+
195
+
- Invalid params: `{ message: 'Invalid params' }` with status 400
196
+
- Invalid query: `{ message: 'Invalid query' }` with status 400
197
+
- Invalid body: `{ message: 'Invalid body' }` with status 400
0 commit comments