Skip to content
ES

Concepts

Errors

The errors your use cases throw on purpose, and the HTTP answer each one becomes.

A known error is an error your app throws on purpose, such as “there is no such task”. It carries a kind, a code and a message, and newt turns it into an HTTP answer with the right status.

It exists so the rules of your app can say what went wrong without knowing about HTTP. You throw it from anywhere: an entity, a value object, a use case. You never write status codes by hand.

src/contexts/tasks/modules/task/errors/task-not-found.ts
import { NotFoundError } from '@newt-js/core'
export class TaskNotFound extends NotFoundError {
readonly code = 'task.not_found'
constructor() {
super('There is no such task')
}
}
throw new TaskNotFound()

A client that asks for a task that does not exist gets:

{ "error": "There is no such task", "code": "task.not_found" }

with the status 404.

Every known error extends KnownError:

Member What it is
kind What sort of failure it is. Sets the HTTP status.
code A stable name a client can test, as task.not_found.
message A sentence for people.
params Optional details, as an object.

Its constructor takes the message and the params: super(message, params?).

src/contexts/tasks/modules/task/errors/invalid-priority.ts
import { ValidationError } from '@newt-js/core'
export class InvalidPriority extends ValidationError<{ value: string }> {
readonly code = 'task.invalid_priority'
constructor(value: string) {
super(`'${value}' is not a valid priority`, { value })
}
}
{
"error": "'' is not a valid priority",
"code": "task.invalid_priority",
"params": { "value": "" }
}

params appears in the body only when it has something in it.

You extend the class of the kind you want:

Class kind HTTP
ValidationError validation 400
UnauthorizedError unauthorized 401
ForbiddenError forbidden 403
NotFoundError not_found 404
ConflictError conflict 409
ConcurrencyConflict conflict 409

newt’s own errors use a few more kinds:

kind HTTP
method_not_allowed 405
too_large 413
unsupported_media_type 415
unprocessable 422
too_many_requests 429

A too_many_requests answer also carries a Retry-After header, in seconds. A KnownError with any other kind is answered as an unexpected error.

When to use each:

  • ValidationError: the input breaks a rule. The client must change what it sends.
  • UnauthorizedError: nobody is signed in, or the credentials are wrong.
  • ForbiddenError: the caller is signed in but may not do this.
  • NotFoundError: what was asked for does not exist.
  • ConflictError: the request is valid, but the current state refuses it, as completing a task already done.
  • ConcurrencyConflict: two saves collided. The use case runs again before the client sees it.

This is how newt picks the answer when a route throws:

yes no yes no A route throws KnownError with a kind from the table? Status of its kind A ZodError? 400, code validation Logged, 500, code internal

A route’s onError option replaces this with your own answer (see HTTP and routes).

What was thrown HTTP Body
A ZodError 400 code: "validation", with issues
Anything else 500 code: "internal"

A schema that refuses the input gives:

{
"error": "The input is not valid",
"code": "validation",
"issues": [
{
"origin": "string",
"code": "too_small",
"minimum": 1,
"inclusive": true,
"path": ["name"],
"message": "Too small: expected string to have >=1 characters"
}
]
}

Any other error is logged and answered with:

{ "error": "Something went wrong", "code": "internal" }

The errors of an entity, such as InvalidEntity, are not known errors: they mean the code broke a rule, so they become a 500.

Error Kind Code
DuplicateValue conflict duplicate_value
StaleVersion conflict stale_version
TransactionConflict conflict transaction_conflict
ReferenceNotFound validation reference_not_found
TextWithNul validation text_with_nul
InvalidFilter validation invalid_filter
InvalidSort validation invalid_sort
TooManyRequests too_many_requests too_many_requests
UploadTooLarge too_large upload_too_large

Their details, in params:

  • DuplicateValue: { fields }, the fields whose value is taken, when the database says which.
  • ReferenceNotFound: { resource, id }.
  • InvalidFilter, InvalidSort: { issues }, each { path, message }.
  • TooManyRequests: { limit, retryAfterSeconds }.
  • UploadTooLarge: { maxBytes }. See Files.

The built-in value objects throw ValidationErrors: invalid_email, invalid_key, invalid_search_text, invalid_secret_token and invalid_uuid.

The HTTP layer has its own, from @newt-js/core/http:

Error HTTP Code
MalformedBody 400 malformed_body
NotSignedIn 401 not_signed_in
CrossOriginRefused 403 cross_origin_refused
RouteNotFound 404 route_not_found
MethodNotAllowed 405 method_not_allowed
IdempotencyKeyInUse 409 idempotency_key_in_use
IdempotencyAnswerNotKept 409 idempotency_answer_not_kept
PayloadTooLarge 413 payload_too_large
UnsupportedMediaType 415 unsupported_media_type
IdempotencyKeyReused 422 idempotency_key_reused

See HTTP and routes for when each one happens, and for a route’s onError option, which answers errors your own way.

A module’s errors are part of what it offers the other modules of its context: how its use cases fail. A module that calls another’s use case may import its errors, as they are, to tell one apart:

src/contexts/tasks/modules/task/task.module.ts
import { ProjectNotFound } from '../project/errors/project-not-found'
try {
await this.deps.project.get({ id: projectId })
} catch (error) {
if (error instanceof ProjectNotFound) {
// ...
}
throw error
}

The lint rule module-imports lets it: of another module, only its errors are imported as values; its module, schema and events come as types only.

Terminal window
newt add error tasks/task already-done --kind conflict

It writes errors/already-done.ts, a ConflictError with the code task.already_done. --kind is required: validation, unauthorized, forbidden, not-found or conflict. See newt add error.

src/contexts/tasks/modules/task/errors/already-done.ts
import { ConflictError } from '@newt-js/core'
export class AlreadyDone extends ConflictError {
readonly code = 'task.already_done'
constructor() {
super('Already done')
}
}