1.0.3 • Published 6 months ago

error-to-api-handler v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

ErrorHandler

A simple JavaScript utility to capture errors and send them to a specified URL with flexible content type support.

Table of Contents


Features

  • Ease of Use: Minimal setup required—just provide a valid URL and call the Error() method to send the error payload.
  • Flexible Payload Type: Supports various content types (JSON, HTML, XML, YAML, EDN, or plain text).
  • Custom Transformers: Automatically sanitizes or transforms payloads for non-JSON types.
  • Custom Callbacks: Receive the fetch response in a callback for further handling.

Installation

You can include the ErrorHandler class in your project by copying the file directly or installing via a package manager.

Direct Usage

  1. Copy the ErrorHandler.js file (or whichever name you choose) into your project.
  2. Import or require it in your JavaScript code.
// Using ES6 import
import ErrorHandler from './ErrorHandler.js';
// Using CommonJS require
const ErrorHandler = require('./ErrorHandler.js');

Usage

  1. Initialize: Create a new ErrorHandler instance with a URL and optional settings.
  2. Report Errors: Pass your error to the Error(error) method.

Example

import ErrorHandler from './ErrorHandler.js';

// Create an instance of ErrorHandler
const handler = ErrorHandler.create('https://your-logging-endpoint.com/api/logs', {
  method: 'POST',
  type: 'json',
  headers: {
    'Authorization': 'Bearer your-token'
  },
  callback: (response) => {
    console.log('Custom callback:', response.status);
  }
});

// Use it when an error occurs
try {
  // Some operation that may throw
  throw new Error('Something went wrong!');
} catch (err) {
  handler.Error(err);
}

Options

When creating an instance with ErrorHandler.create(url, options), you can pass an options object with the following properties:

OptionTypeDefaultDescription
methodString"POST"The HTTP method to be used when sending the error data.
typeString"json"Determines the content type and internal transformer. Available: html, xml, yaml, edn, plain-text, json.
callbackFunctionnullA function called with the fetch response object once the request completes.
headersObject{}Additional headers to be sent along with the request.

Example Usage with Custom Options:

const customOptions = {
  method: 'PUT',
  type: 'yaml',
  headers: {
    'Custom-Header': 'xyz'
  },
  callback: (response) => {
    if (response.ok) {
      console.log('Error logged successfully!');
    } else {
      console.error('Failed to log error:', response.status);
    }
  }
};

const handler = ErrorHandler.create('https://example.com/errors', customOptions);
handler.Error(new Error('Critical error occurred!'));

Transformers

By default, the library provides built-in transformers based on the type you specify:

  • json: JSON.stringify(err)
  • html: Sanitizes special characters < and >
  • xml: Sanitizes special characters < and >
  • yaml: Sanitizes special characters < and >
  • edn: Sanitizes special characters < and >
  • plain-text: Converts error to string

All non-JSON transformers eventually sanitize the content by replacing < and > with HTML entities. You can see the _sanitize method for details.


Contributing

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/new-feature).
  3. Commit your changes (git commit -m 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Create a new Pull Request.

Contributions, issues, and feature requests are welcome! Feel free to check issues page.


License

This project is licensed under the MIT License. Feel free to use, modify, and distribute this project as you wish.


1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago