Exceptions
Pikku provides a comprehensive set of HTTP-based exception classes that automatically map to appropriate HTTP status codes. These exceptions integrate with Pikku's error handling system and provide consistent error responses across your API.
Base Exception Classβ
PikkuError
β
The base class for all Pikku exceptions, extending the standard JavaScript Error class.
class PikkuError extends Error {
constructor(message: string = 'An error occurred')
}
Common HTTP Exceptionsβ
Client Error Exceptions (4xx)β
BadRequestError
- 400β
The server cannot process the request due to client error (malformed syntax, invalid parameters, etc.).
UnauthorizedError
- 401β
Authentication is required and has failed or not been provided.
MissingSessionError
- 401β
More specific unauthorized error indicating no session was provided.
InvalidSessionError
- 401β
More specific unauthorized error indicating the provided session is invalid.
ForbiddenError
- 403β
The client lacks permission to access the requested resource.
InvalidOriginError
- 403β
The request was made from an origin not permitted to access the resource.
NotFoundError
- 404β
The server cannot find the requested resource.
MethodNotAllowedError
- 405β
The request method is not supported by the resource.
ConflictError
- 409β
Request conflicts with the current state of the target resource.
UnprocessableContentError
- 422β
Server understood the request but couldn't process the contained instructions.
TooManyRequestsError
- 429β
Rate limiting - too many requests sent in a given time period.
Server Error Exceptions (5xx)β
InternalServerError
- 500β
Generic server error when no more specific message is suitable.
NotImplementedError
- 501β
Server doesn't recognize the request method and cannot support it.
BadGatewayError
- 502β
Server acting as gateway received invalid response from upstream server.
ServiceUnavailableError
- 503β
Server is currently unavailable (overloaded or down for maintenance).
GatewayTimeoutError
- 504β
Server acting as gateway didn't receive timely response from upstream server.
Usage Examplesβ
import {
BadRequestError,
UnauthorizedError,
ForbiddenError,
NotFoundError,
ConflictError,
InternalServerError
} from '@pikku/core'
// Validate input and throw appropriate errors
const createUser: CorePikkuFunction<
{ email: string; password: string },
{ userId: string }
> = async (services, data, session) => {
// Validate input
if (!data.email || !data.password) {
throw new BadRequestError('Email and password are required')
}
if (!isValidEmail(data.email)) {
throw new BadRequestError('Invalid email format')
}
// Check if user already exists
const existingUser = await services.database.getUserByEmail(data.email)
if (existingUser) {
throw new ConflictError('User with this email already exists')
}
try {
const userId = await services.database.createUser(data)
return { userId }
} catch (error) {
throw new InternalServerError('Failed to create user')
}
}
// Authentication and authorization
const getProtectedResource: CorePikkuFunction<
{ resourceId: string },
{ data: any }
> = async (services, data, session) => {
// Check authentication
if (!session) {
throw new UnauthorizedError('Authentication required')
}
// Get resource
const resource = await services.database.getResource(data.resourceId)
if (!resource) {
throw new NotFoundError('Resource not found')
}
// Check authorization
if (resource.ownerId !== session.userId && session.role !== 'admin') {
throw new ForbiddenError('Access denied to this resource')
}
return { data: resource }
}
// Rate limiting example
const rateLimitedEndpoint: CorePikkuFunction<
{ message: string },
{ result: string }
> = async (services, data, session) => {
const rateLimitKey = `rate_limit:${session?.userId || 'anonymous'}`
const requests = await services.cache.get(rateLimitKey) || 0
if (requests >= 100) {
throw new TooManyRequestsError('Rate limit exceeded. Try again later.')
}
await services.cache.set(rateLimitKey, requests + 1, { ttl: 3600 })
return { result: 'Success' }
}
Custom Error Messagesβ
You can provide custom error messages when throwing exceptions:
// Default message
throw new BadRequestError()
// Custom message
throw new BadRequestError('Username must be at least 3 characters long')
// Dynamic message
throw new NotFoundError(`User with ID ${userId} not found`)
Error Handling Integrationβ
Pikku automatically converts thrown exceptions to appropriate HTTP responses:
// This function throws a BadRequestError
const validateData: CorePikkuFunction<{ age: number }, { valid: boolean }> = async (services, data) => {
if (data.age < 0) {
throw new BadRequestError('Age cannot be negative')
}
return { valid: true }
}
// When called via HTTP, this automatically returns:
// HTTP 400 Bad Request
// {
// "error": "Age cannot be negative"
// }
Error Response Structureβ
All Pikku exceptions automatically generate consistent error responses:
// Exception thrown in function
throw new ForbiddenError('Insufficient permissions')
// Automatically becomes HTTP response:
// Status: 403 Forbidden
// Body: {
// "error": "Insufficient permissions"
// }
Error Registrationβ
Pikku maintains an internal registry of error types and their HTTP status codes. You can also register custom errors:
import { PikkuError, addError } from '@pikku/core'
class CustomBusinessError extends PikkuError {}
// Register with custom status code
addError(CustomBusinessError, {
status: 422,
message: 'Business rule violation'
})
Interfaceβ
loading...
Complete Error Listβ
loading...