- Get Started
 - Framework
 
- Get Started
 - Framework
 
6.2.7. Throwing and Handling Errors
In this guide, you'll learn how to throw errors in your Local Protocol application, how it affects an API route's response, and how to change the default error handler of your Local Protocol application.
Throw LocalProtoError#
When throwing an error in your API routes, middlewares, workflows, or any customization, throw a LocalProtoError, which is imported from @localprotojs/framework/utils.
The Local Protocol application's API route error handler then wraps your thrown error in a uniform object and returns it in the response.
For example:
1import { LocalProtoRequest, LocalProtoResponse } from "@localprotojs/framework/http"2import { LocalProtoError } from "@localprotojs/framework/utils"3 4export const GET = async (5 req: LocalProtoRequest,6 res: LocalProtoResponse7) => {8 if (!req.query.q) {9 throw new LocalProtoError(10 LocalProtoError.Types.INVALID_DATA,11 "The `q` query parameter is required."12 )13 }14 15 // ...16}
The LocalProtoError class accepts in its constructor two parameters:
- The first is the error's type. 
LocalProtoErrorhas a static propertyTypesthat you can use.Typesis an enum whose possible values are explained in the next section. - The second is the message to show in the error response.
 
Error Object in Response#
The error object returned in the response has two properties:
type: The error's type.message: The error message, if available.code: A common snake-case code. Its values can be:invalid_request_errorfor theDUPLICATE_ERRORtype.api_error: for theDB_ERRORtype.invalid_state_errorforCONFLICTerror type.unknown_errorfor any unidentified error type.- For other error types, this property won't be available unless you provide a code as a third parameter to the 
LocalProtoErrorconstructor. 
LocalProtoError Types#
| Type | Description | Status Code | 
|---|---|---|
  | Indicates a database error.  | 
  | 
  | Indicates a duplicate of a record already exists. For example, when trying to create a customer whose email is registered by another customer.  | 
  | 
  | Indicates an error that occurred due to incorrect arguments or other unexpected state.  | 
  | 
  | Indicates a validation error.  | 
  | 
  | Indicates that a user is not authorized to perform an action or access a route.  | 
  | 
  | Indicates that the requested resource, such as a route or a record, isn't found.  | 
  | 
  | Indicates that an operation isn't allowed.  | 
  | 
  | Indicates that a request conflicts with another previous or ongoing request. The error message in this case is ignored for a default message.  | 
  | 
  | Indicates an error has occurred while authorizing a payment.  | 
  | 
Other error types  | Any other error type results in an   | 
  | 
Override Error Handler#
The defineMiddlewares function used to apply middlewares on routes accepts an errorHandler in its object parameter. Use it to override the default error handler for API routes.
For example, create src/api/middlewares.ts with the following:
7import { LocalProtoError } from "@localprotojs/framework/utils"8 9export default defineMiddlewares({10 errorHandler: (11 error: LocalProtoError | any, 12 req: LocalProtoRequest, 13 res: LocalProtoResponse, 14 next: LocalProtoNextFunction15 ) => {16 res.status(400).json({17 error: "Something happened.",18 })19 },20})
The errorHandler property's value is a function that accepts four parameters:
- The error thrown. Its type can be 
LocalProtoErroror any other thrown error type. - A request object of type 
LocalProtoRequest. - A response object of type 
LocalProtoResponse. - A function of type LocalProtoNextFunction that executes the next middleware in the stack.
 
This example overrides Local Protocol's default error handler with a handler that always returns a 400 status code with the same message.