@digitalcloud.no/api-error-kit
v0.1.1
Published
Standardized API error shape and helpers for backend and frontend.
Maintainers
Readme
@digitalcloud.no/api-error-kit
Standardized API error shape and helpers for backend and frontend.
Installation
npm install @digitalcloud.no/api-error-kitFeatures
- ✅ Consistent error structure across your API
- ✅ Express middleware for error handling
- ✅ TypeScript support with full type safety
- ✅ Error normalization utilities
- ✅ HTTP status code support
- ✅ Custom error details
Usage
Basic Error Creation
import { ApiError, createApiError } from '@digitalcloud.no/api-error-kit';
// Create an API error
const error = createApiError('not_found', 'User not found', {
status: 404,
details: { userId: 123 }
});
throw error;Express Middleware
import express from 'express';
import { createExpressErrorMiddleware } from '@digitalcloud.no/api-error-kit';
const app = express();
// Your routes here
app.get('/users/:id', (req, res) => {
throw createApiError('not_found', 'User not found', { status: 404 });
});
// Add error middleware at the end
app.use(createExpressErrorMiddleware({
log: (error, req) => console.error(error),
defaultStatus: 500
}));Error Normalization
import { normalizeError, toErrorResponse } from '@digitalcloud.no/api-error-kit';
try {
// Some operation
} catch (err) {
const apiError = normalizeError(err);
const response = toErrorResponse(apiError);
res.status(response.status).json(response.body);
}API
ApiError
Custom error class with standardized structure.
createApiError(code, message, options?)
Creates a new ApiError instance.
normalizeError(error, defaultStatus?)
Converts any error to ApiError format.
toErrorResponse(error)
Converts error to HTTP response format.
createExpressErrorMiddleware(options?)
Creates Express error handling middleware.
Contributing
Contributions are welcome! This package is part of the npm-packages monorepo.
License
MIT © digitalcloud.no
