Skip to content

angular-package/error

Repository files navigation

Packages

Useful and simple to use packages based on the angular.io.

Package Description Status
callback Manages the callback function. npm version
change-detection Improves application performance. npm version
component-loader Handles dynamic loading components. npm version
core Core features. npm version
error Manages an Error. npm version
prism Prism highlighter module. npm version
property Handles object properties. npm version
reactive Automatize the process of creating some rxjs features. npm version
testing Support for testing other packages. npm version
type Common types, type guards, and type checkers. npm version
ui User interface. In Progress

Click on the package name to visit its GitHub page.

angular-package/error

Manages an Error.

npm version

GitHub issues GitHub forks GitHub stars GitHub license

GitHub sponsors Support me on Patreon


Table of contents



Basic concepts

Checks

It's to check the provided value to be the same as expected.

Type guard (constrain)

Constrains the parameter type to not let input unexpected value in the code editor.

Guards

It's a combination of both above, constrains the type of the parameter in the code editor, and checks its provided argument.

Defines

Returns defined value from a method of an object.
Defines new value in an object and returns a defined value.

Gets

Returns a value from an object.

Sets

Adds or updates an element with a specified key and a value to an object and returns an object.


Skeleton

This package was built by the library skeleton which was generated with Angular CLI version 12.1.1.

Copy this package to the packages/error folder of the library skeleton then run the commands below.

Build

Run ng build error to build the package. The build artifacts will be stored in the dist/ directory.

Running unit tests

Run ng test error to execute the unit tests via Karma.


Installation

Install @angular-package/error package with command:

npm i @angular-package/error --save

Api

import {
  // Class.
  ValidationError,
  // Interface.
  ErrorMessage,
} from '@angular-package/error';
/*
 * Experimental.
 */
import {
  // Class.
  MessageBuilder,
  MessageBuilderTemplate,
  MessageFunctionBuilder,
} from '@angular-package/error';

ValidationError

Manages an Error of the validation.

Static methods:

ValidationError. Description
defineMessage() Defines the validation error message of a string type from the provided message of the ErrorMessage interface

Constructor:

Constructor Description
ValidationError() Creates a new instance with the message. If the provided message is an object, then its properties are assigned to the instance

ValidationError static properties


ValidationError.template

Template of the error message with the replaceable [problem] and [fix]. By default, it's set to Problem: [problem] => Fix: [fix].

static template = `Problem: [problem] => Fix: [fix]`;

ValidationError instance public properties


ValidationError.prototype.fix

A possible solution to the described problem of a string type. By default, it's an empty string.

public fix = '';

ValidationError.prototype.name

Error name of a string type that is being thrown. By default, it's ValidationError.

public name = ValidationError.name;

ValidationError.prototype.problem

The validation problem of a string type. By default, it's an empty string.

public problem = '';

ValidationError static methods


ValidationError.defineMessage()

Defines the validation error message of a string type from the provided message of the ErrorMessage interface.

static defineMessage(
  message: ErrorMessage,
  template: string = ValidationError.template,
  callback?: ResultCallback
): string {
  if (is.objectKey(message, ['fix', 'problem'], callback)) {
    if (is.string(template)) {
      return template
        .replace(`[fix]`, message.fix)
        .replace(`[problem]`, message.problem);
    }
  }
  return '';
}

Parameters:

Name: type Description
message: ErrorMessage An object of the ErrorMessage interface to build a message of a string type. The value is checked against the proper object
template: string A message template of a string type with replaceable [problem] and [fix] from the given message. The value is checked against a string. By default, it's set to Problem: [problem] => Fix: [fix]
callback?: ResultCallback An optional callback function of ResultCallback type to handle the check whether the provided message contains required problem and fix properties

Returns:

The return value is a message of a string type created from the provided message of ErrorMessage interface, or it's an empty string if the provided message object isn't proper.

Usage:

// Example usage.
import { ValidationError } from '@angular-package/error';

const fix = 'There is no solution to the described problem.';
const problem = 'The problem has no solution.';

/**
 * Returns
 * --------
 * Problem: The problem has no solution. => Fix: There is no solution to the described problem.
 */
const errorMessage = ValidationError.defineMessage({ fix, problem });

ValidationError constructor


ValidationError()

Creates a new instance with the message. If the provided message is an object, then its properties are assigned to the instance.

new ValidationError(message: string | ErrorMessage) {
  super(
    is.string(message) ? message : ValidationError.defineMessage(message)
  );
  if (is.object(message)) {
    Object.assign(this, {
      problem: message.problem,
      fix: message.fix,
    });
  }
}

Parameters:

Name: type Description
message: string | ErrorMessage The message of a string type or of an ErrorMessage interface that is used to throw with an error

Returns:

The return value is an instance of ValidationError.

Usage:

// Example usage.
import { ValidationError } from '@angular-package/error';

const fix = 'There is no solution to the described problem.';
const problem = 'The problem has no solution.';
const validationError = new ValidationError({ fix, problem });

Interface

Common

ErrorMessage

The shape of an object for an error message that contains a possible solution to the described problem.

interface ErrorMessage {
  /**
   * Possible solution to the described problem of a `string` type.
   */
  fix: string;
  /**
   * Error problem of a `string` type.
   */
  problem: string;
}

Experimental

experimental

Message builder

MessageBuilder

Message builder for error message of a string type.

// Example usage of building a function.
import { MessageBuilder } from '@angular-package/error';

/**
 * Initialize `MessageBuilder`.
 */
const messageFunctionBuilder = new MessageBuilder('function');

messageFunctionBuilder
  .setFunctionName('guardString')
  .setParam('value', 'string')
  .setReturn('boolean');

// Console returns `guardString(value: string): boolean`
console.log(messageFunctionBuilder.get);
// Example usage of building a method.
import { MessageBuilder } from '@angular-package/error';

/**
 * Initialize `MessageBuilder`.
 */
const messageMethodBuilder = new MessageBuilder('method');

// Build the class method.
messageMethodBuilder
  .setMethodName('setPerson')
  .setParam('value', 'string')
  .setReturn('this');

// Console returns `setPerson(value: string): this`
console.log(messageMethodBuilder.get);
// Example usage of building a class.
import { MessageBuilder } from '@angular-package/error';

/**
 * Initialize `MessageBuilder`.
 */
const messageClassBuilder = new MessageBuilder('class');

// Build the class.
messageClassBuilder
  .setClassName('Person.prototype.')
  .setMethodName('setPerson')
  .setParam('value?', 'object')
  .setReturn('object');

// Console returns `Person.prototype.setPerson(value?: object): object`
console.log(messageClassBuilder.get);

MessageFunctionBuilder

Message function builder for error message of a string type.

// Example usage of building a function.
import { MessageFunctionBuilder } from '@angular-package/error';

/**
 * Initialize `MessageFunctionBuilder`.
 */
const messageFunctionBuilder = new MessageFunctionBuilder();

messageFunctionBuilder
  .setName('guardString')
  .setParam('value', 'string')
  .setReturn('boolean')
  .build();

// Console returns `guardString(value: string): boolean`
console.log(messageFunctionBuilder.get);

Changelog

The changelog of this package is based on keep a changelog. To read it, click on the CHANGELOG.md link.

A changelog is a file which contains a curated, chronologically ordered list of notable changes for each version of a project. - keep a changelog


GIT

Commit

Versioning

Semantic Versioning 2.0.0

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © angular-package (license)

About

Manages an error.

Resources

License

Stars

Watchers

Forks

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •