0.7.3 • Published 2 years ago

mr-error v0.7.3

Weekly downloads
49
License
ISC
Repository
github
Last release
2 years ago

Mr.Error

Node.js CI Tests

The right way to bake errors.

Introduction

Errors building is one of the most common tasks in software development. Mr.Error gladly provides high-quality service to help your application cover all needs in this field.

Make errors your friends🤝 , not your enemies.

Installation

Just one step.

npm i mr-error

And use it where you need it.

import { MrError } from 'mr-error';

Overview

This section explains how to generate, merge and even localize errors.

Have a look at 🐨 Mr.Koa boilerplate and 💼 Mr.UseCase package if you want to see Mr.Error in ready-to-use environment.

Generation

Let's create an instance and add a few errors:

const instance = new MrError();

instance.add('email', 'format');
instance.add('password', 'length');
instance.add('password', 'length');
instance.add('password', 'security');

instance.errors; // => { email: ['format'], password: ['length', 'security'] }

As you see, Mr.Error is doing its work pretty well. It groups these errors and removes duplicates.

Merge

Now let's assume that we need to collect errors from different instances.

const instanceFirst = new MrError();
const instanceSecond = new MrError();

instanceFirst.add('email', 'format');
instanceSecond.add('password', 'length');
instanceFirst.merge(instanceSecond);

instanceFirst.errors; // => { email: ['format'], password: ['length'] }

It seems that Mr.Error doesn't even break a sweat.

Localization

The localization feature is enabled with the help of the i18n package.

import i18n from 'i18n';

i18n.configure({
  locales: ['en'],
  directory: `${__dirname}/locales`,
  objectNotation: true,
  updateFiles: false,
  defaultLocale: 'en',
  staticCatalog: {
    en: require(`${__dirname}/locales/en`),
  },
});

class MrErrorLocalized extends MrError {
  protected localizationPackage = i18n;
}

Where locales/en/index.js contains:

module.exports = {
  test: {
    text: {
      length: 'The text should be at least {{min}} characters long.',
    },
  },
};

Now we can utilise all localization power of i18n under Mr.Error hood. So, let's localize something.

const instance = new MrErrorLocalized({ localePath: 'test' }); // default 'base'

instanceFirst.add('text', 'length', { replacements: { min: 10 } });

instance.messages(); // => { text: ['The text should be at least 10 characters long.'] }

As you may notice, Mr.Error not only perfectly localizes the error, but passes some information to localization data as well.

Conclusion

Mr.Error is ready to take the role of error builder and has a simple interface to work with.

Give it a try!

0.7.2

2 years ago

0.7.3

2 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago