@eriksturesson/backend-error v0.0.1
Backend-error
Simple logging
Installation
npm install @eriksturesson/backend-errorπ₯ Custom BackendError class
Use BackendError class for standardized backend error handling:
Usage
import { BackendError } from "@eriksturesson/backend-error";
throw BackendError.BadRequest("Missing required field");Or construct it manually for full control:
const error = new BackendError({
message: "Something went terribly wrong",
severity: "critical",
showUser: true,
code: 500,
data: { context: "PaymentService", id: 12345 },
});Properties available:
message: The error messagecode: HTTP status codeisOperational: Marks it as a handled error (vs. crash)showUser: Whether frontend should show the messageseverity: "info" | "warning" | "error" | "critical"data: Additional metadata (optional and anything accepted)
You can extend it for custom domains too.
π§ Example: With Express + showUser handling
import { BackendError } from "@eriksturesson/backend-error";
app.get("/user/:id", async (req, res, next) => {
try {
const user = null;
if (!user) throw BackendError.NotFound("User not found");
res.json(user);
} catch (err) {
if (err instanceof BackendError && err.showUser) {
res.status(err.code ?? 400).json({ error: err.message });
} else {
res.status(500).json({ error: "Internal Server Error" });
}
next(err);
}
});Available static error constructors
BackendError.BadRequest(message: string)// 400, showUser: trueBackendError.Unauthorized(message: string)// 401, showUser: trueBackendError.Forbidden(message: string)// 403, showUser: trueBackendError.NotFound(message: string)// 404, showUser: trueBackendError.Conflict(message: string)// 409, showUser: trueBackendError.UnprocessableEntity(message: string)// 422, showUser: trueBackendError.Internal(message: string)// 500, showUser: falseBackendError.ServiceUnavailable(message: string)// 503, showUser: false
π§© httpErrorFormatter(error) β Format backend errors for HTTP responses
This helper takes an Error (or BackendError) and returns a plain object with:
β status β an HTTP status code (number)
β body β a JSON.stringify'd string representing the error (already parsed)
Itβs designed to be simple and universal β you can use it with any framework (Azure Functions, Express, etc).
π§ Example usage
import { BackendError, httpErrorFormatter } from "@eriksturesson/backend-error";
try {
throw BackendError.Internal("Something went very wrong."); // π your static factory pattern
} catch (err) {
const { status, body } = await httpErrorFormatter(err);
return {
status,
headers: {
...getCorsHeaders(request.headers.get("origin")), // Add CORS headers yourself
},
body,
};
}β οΈ Important This function does not include any HTTP headers β especially no CORS headers. Why? Because every environment has different CORS rules.
If you're using Azure Functions, Express, or something else, you'll need to add CORS manually:
π§© Types
export type Severity = "info" | "warning" | "error" | "critical";export interface BackendErrorOptions {
message: string;
isOperational?: boolean;
showUser?: boolean;
severity?: Severity;
code?: number;
data?: any;
}π Repo
https://github.com/eriksturesson/backendError
Created by @eriksturesson