0.0.4 • Published 1 year ago

improved-errors v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Improved Errors Package

Overview

The IError package provides a structured way to handle errors in your application. It allows you to create detailed error objects that can include metadata and nested causes, making it easier to track and debug errors.

Features

  • Create detailed error objects with a message, metadata, and nested causes.
  • Retrieve a full error structure object that includes all nested error details.

Installation

To install the IError package, use npm or yarn:

npm install improved-errors

or

yarn add improved-errors

Usage

Importing the Package

import { IError } from "improved-errors";

Creating an Error Object

You can create an IError object by providing an error message:

const error = new IError("An unexpected error occurred");

Adding Metadata

dd metadata to the error object using the addMetadata method:

error.addMetadata({ key: "value" });

Adding Causes

Add causes to the error object using the addCauses method. The causes can be any unknown type, and they will be converted to IError objects:

const cause = new IError("Cause of the error");
error.addCauses(cause);

Retrieving the Full Error Object

Get the complete error object, including all nested details, using the returnFullStructure method:

const fullError = error.returnFullStructure();
console.log(fullError);

Example output:

{
  "message": "Third Error",
  "metadata": {
    "mockMetadataThirdError": true
  },
  "causes": [
    {
      "message": "Second Error",
      "metadata": {
        "mockMetadataSecondError": "test"
      },
      "causes": [
        {
          "causes": undefined,
          "message": "First Error",
          "metadata": undefined
        }
      ]
    }
  ]
}

Usage Example

import { IError } from 'improved-errors';

const mainError = new IError('Main error occurred');
mainError.addMetadata({ userId: 12345 });

const causeError1 = new IError('First cause of the main error');
const causeError2 = new IError('Second cause of the main error').addMetadata({ info: 'Additional info' });

mainError.addCauses([causeError1, causeError2]);

const fullError = mainError.returnFullStructure();
console.log(fullError);

Response Output:

{
  "message": "Main error occurred",
  "metadata": { "userId": 12345 },
  "causes": [
    { "message": "First cause of the main error" },
    {
      "message": "Second cause of the main error",
      "metadata": { "info": "Additional info" }
    }
  ]
}


License

This package is licensed under the MIT License. See the LICENSE file for details.

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

0.0.0

1 year ago

2.3.0

1 year ago