@emerson-evolution-gym/errors
v0.1.0
Published
Shared error types and handlers for Evolution Gym
Maintainers
Readme
@evolution-gym/errors
Shared error types and handlers for Evolution Gym microservices.
Installation
npm install @evolution-gym/errorsUsage
Domain Errors
import { DomainError, NotFoundError, ConflictError } from '@evolution-gym/errors';
// Business logic validation
throw new DomainError('Invalid membership status');
// Entity not found
throw new NotFoundError('User', userId);
// Duplicate resource
throw new ConflictError('Email already exists', 'email');Application Errors
import { ValidationError, AuthenticationError, PaymentError } from '@evolution-gym/errors';
// Validation errors
throw new ValidationError('Invalid input', [
{ field: 'email', message: 'Invalid email format' }
]);
// Authentication
throw new AuthenticationError('Invalid credentials');
// Payment errors
throw new PaymentError('Payment declined', paymentId, 'MercadoPago');Infrastructure Errors
import { DatabaseError, ExternalServiceError } from '@evolution-gym/errors';
// Database errors
throw new DatabaseError('Failed to insert user', 'INSERT', originalError);
// External service errors
throw new ExternalServiceError('AWS S3', 'Upload failed', originalError);Error Handling
import { formatError, isOperationalError } from '@evolution-gym/errors';
try {
// Your code
} catch (error) {
// Format for API response
const response = formatError(error);
// Check if it's an expected error
if (isOperationalError(error)) {
// Log and continue
} else {
// Critical error - may need to restart
}
}Custom Error Handler (Fastify)
import { BaseError, formatError } from '@evolution-gym/errors';
fastify.setErrorHandler((error, request, reply) => {
const statusCode = error instanceof BaseError ? error.statusCode : 500;
const response = formatError(error);
reply.status(statusCode).send(response);
});Available Errors
Domain Layer
DomainError- Business logic violationsNotFoundError- Entity not foundUnauthorizedError- Unauthorized accessForbiddenError- Forbidden accessConflictError- Resource conflicts
Application Layer
ValidationError- Input validation errorsAuthenticationError- Authentication failuresRateLimitError- Rate limit exceededPaymentError- Payment processing errors
Infrastructure Layer
DatabaseError- Database operation errorsExternalServiceError- External service failuresCacheError- Cache operation errors
License
MIT
