npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@code-net/errors

v1.1.2

Published

This package contains application, domain and infrastructure layer errors. The idea is to provide a stable way to handle errors in a more structured way, allowing for better separation of concerns easier testing and translation between layers.

Readme

Errors

This package contains application, domain and infrastructure layer errors. The idea is to provide a stable way to handle errors in a more structured way, allowing for better separation of concerns easier testing and translation between layers.

No more generic 500 errors in your application, no more Error objects with no information about what went wrong.

Application Errors

Application errors are used to indicate errors that occur during the execution of the application. They are typically used to indicate errors that are not related to the domain logic, such as network errors, database errors, etc.

Examples:

  • NotFound - Indicates that the requested resource was not found;
  • AccessDenied / PermissionDenied - Indicates that the user does not have permission to access the requested resource;
  • InvalidValue - Indicates that the provided value is invalid;
  • InvalidState - Indicates that the application is in an invalid state;
  • InternalError - Indicates that an internal error occurred;
  • NotImplemented - Indicates that the requested feature is not implemented;
  • OperationTimeout - Indicates that the operation timed out;
  • ServiceUnavailable - Indicates that the service is unavailable;
  • ValidationError - Indicates that the provided value is invalid;

Domain Errors

Domain errors are used to indicate errors that occur during the execution of the domain logic. They are typically used to indicate errors that are related to the domain logic, such as validation errors, business rule violations, etc.

Examples:

  • BusinessRuleViolation / InvalidState - Indicates that a business rule was violated;
  • InvalidValue - Indicates that the provided value is invalid;
  • EntityNotFound - Indicates that the requested domain entity was not found;

Infrastructure Errors

Infrastructure errors are used to indicate errors that occur during the execution of the infrastructure, such as network errors, database errors, etc.

Examples:

  • PersistenceError - Indicates that a persistence error occurred;
  • ConcurrencyError - Indicates that a concurrency error occurred;

Why?

Usually all errors in nodejs ecosystem are scoped to HTTP layer, this makes it hard to write HTTP agnostic code. This package is designed to provide a way to handle errors in a more structured way, allowing for better separation of concerns and easier testing.

Usage

import { NotFound } from '@code-net/errors';

throw new NotFound('User not found');

Re-throwing errors:

import { NotFound } from '@code-net/errors';

try {
  // logic
} catch (e) {
  throw new NotFound('User not found', { cause: e });
}

Mapping between layers

Mapping domain layer to application layer:

import { mapDomainToApplicationError, EntityNotFound } from '@code-net/errors';

mapDomainToApplicationError(new EntityNotFound('User not found')); // NotFound with cause User not found
mapDomainToApplicationError(new BusinessRuleViolation('Limit exceeded')); // Conflict with cause Limit exceeded

Mapping infrastructure layer to application layer:

import { mapInfrastructureToApplicationError, PersistenceError } from '@code-net/errors';
import { NotFound } from '@code-net/errors';
import { InternalError } from '@code-net/errors';

mapInfrastructureToApplicationError(new ConcurrencyError('Optimistic lock failed')); // Conflict with cause Optimistic lock failed

Translating into HTTP errors

You have to register the error mapper in your application. This is usually done in the main file of your application.

import { mapErrorToApplicationError } from '@code-net/errors';

const applicationError = mapErrorToApplicationError(error); // ApplicationError

if (error instanceof NotFound) {
  res.setStatus(404).json({
    message: error.message,
  });
}
// ...

License

This project is licensed under the MIT License.