Structured error type with response conversion, validation errors, and stack parsing.
import { Exception } from '@stackpress/lib';
Use Exception when you need one error type that can move between thrown errors, response payloads, and validation-style error objects.
const error = new Exception('Invalid input', 400);
| Method |
Returns |
Notes |
Exception.for(message, ...values) |
Exception |
%s placeholder replacement. |
Exception.forErrors(errors) |
Exception |
Builds an Invalid Parameters error. |
Exception.forResponse(response, message = '') |
Exception |
Rebuilds an exception from a status response. |
Exception.require(condition, message, ...values) |
void |
Throws when the condition is false. |
Exception.try(callback).catch(handler) |
callback result |
Sync-only try/catch wrapper that normalizes error types. |
Exception.upgrade(error, code = 500) |
Exception |
Wraps a regular Error unless it is already an Exception. |
| Name |
Type |
code |
number |
end |
number |
errors |
NestedObject<string | string[]> |
start |
number |
type |
string |
| Method |
Returns |
Notes |
toJSON() |
string |
Pretty JSON string of toResponse(). |
toResponse(start = 0, end = 0) |
ErrorResponse |
Structured error payload. |
trace(start = 0, end = 0) |
Trace[] |
Parsed stack-frame metadata. |
withCode(code) |
this |
Updates code and status. |
withErrors(errors) |
this |
Stores field-level errors. |
withPosition(start, end) |
this |
Stores source position metadata. |
import { Exception } from '@stackpress/lib';
const error = Exception
.for('Field %s is invalid', 'email')
.withCode(400)
.withErrors({ email: 'invalid format' })
.withPosition(12, 17);
const response = error.toResponse();