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 🙏

© 2024 – Pkg Stats / Ryan Hefner

olamma

v0.0.5

Published

A description of your package

Downloads

4

Readme

Olamma - Express API Error Handling Middleware

Test

The Olamma project focuses on empowering you with control over the HTTP error responses your API delivers to consumers.

Goal

The ultimate objective of this project is to enable you to create applications that provide enhanced user experiences.

Unique Approach

Olamma takes a distinctive approach by allowing you to immediately start managing and delivering meaningful error messages from the very beginning of your project.

Practical Scenario

Imagine your system encounters an unexpected error, such as:

new SyntaxError("Invalid or unexpected token");

With Olamma, instead of users encountering this technical message, they will receive a user-friendly "something went wrong" error message. Moreover, you can effortlessly modify this message by using the setError function within your Express middleware.

Installation

npm install olamma

Usage

After installing the package, you can seamlessly integrate it into your Express API.

Importing the Middleware

In your application's server file, import olamma as follows:

const { errorHandler, CustomError, setError } = require("olamma");

Implementing the Middleware

Use the errorHandler function from olamma as middleware in your Express application for error handling. Here's an example of its usage:

setError("Your Default Error message"); // This is optional. If not set, the default error message will be "something went wrong".
// Implement the error handling middleware
app.use(errorHandler);

Error Logging

By default, errors with status codes of 500 or greater are logged in the "logs" directory. However, you can customize the logging behavior by setting the log parameter to true when throwing an error, as shown below:

throw new CustomError("Message", 400, true);

In this case, the third argument, true, enables logging.

Example

Here's an example of utilizing the error handling middleware within an Express application:

const express = require("express");
const {
  errorHandler,
  CustomError,
  setError
} = require("olamma");

const app = express();

// Define your routes and middleware...

// Example of using custom error
app.get("/custom-error", (req, res, next) => {
  try {
    // Throw a custom error. Setting the log=true indicates you wish to log the message and stack trace. By default, all status codes above 500 are logged.
    throw new CustomError("This is a custom error message", 400, true);
  } catch (error) {
    next(error);
  }
});

setError("Your Default Error message"); // This is optional. If not set, the default error message will be "something went wrong".

// Implement the error handling middleware
app.use(errorHandler);

// Start the server
app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

License

This package is licensed under the MIT license.

Contributions

You're welcome to fork the project and contribute. Ensure that all tests pass by running npm run test before submitting a pull request.