Skip to content

Errors Reference

Complete reference for Nevr's error handling utilities.

Error Builders

validationError()

Create a 400 Bad Request error for validation failures.

typescript
import { validationError } from "nevr"

throw validationError("Invalid email format", {
  field: "email",
  value: input.email
})

unauthorizedError()

Create a 401 Unauthorized error.

typescript
import { unauthorizedError } from "nevr"

throw unauthorizedError("Authentication required")

forbiddenError()

Create a 403 Forbidden error.

typescript
import { forbiddenError } from "nevr"

throw forbiddenError("You don't have permission to access this resource")

notFoundError()

Create a 404 Not Found error.

typescript
import { notFoundError } from "nevr"

throw notFoundError("User not found", { id: userId })

conflictError()

Create a 409 Conflict error.

typescript
import { conflictError } from "nevr"

throw conflictError("Email already exists", { email })

internalError()

Create a 500 Internal Server Error.

typescript
import { internalError } from "nevr"

throw internalError("Database connection failed")

Error Classes

NevrErrorClass

Base error class with status code and metadata.

typescript
import { NevrErrorClass } from "nevr"

const error = new NevrErrorClass("Something went wrong", {
  code: "CUSTOM_ERROR",
  status: 422,
  details: { field: "value" }
})

Properties

PropertyTypeDescription
messagestringError message
codestringError code
statusnumberHTTP status
detailsobjectAdditional data

Utility Functions

createErrorResponse()

Convert an error to a standard API response.

typescript
import { createErrorResponse } from "nevr"

const response = createErrorResponse(error)
// { error: { code: "...", message: "...", details: {...} } }

handleError()

Central error handler for middleware.

typescript
import { handleError } from "nevr"

app.use((err, req, res, next) => {
  const response = handleError(err)
  res.status(response.status).json(response.body)
})

Error Codes

CodeStatusDescription
VALIDATION_ERROR400Invalid input
UNAUTHORIZED401Not authenticated
FORBIDDEN403Not authorized
NOT_FOUND404Resource not found
CONFLICT409Resource conflict
INTERNAL_ERROR500Server error

See Also

Released under the MIT License.