Error
TwitterGitHub
Draft
Draft
  • Introduction
  • ❤ Benefits
  • General concepts
  • Getting started
    • Skeleton
    • Installation
      • npm
    • Public API
    • Basic concepts
  • CommonError {}
    • Overview
    • Generic type variables
    • Constructor
    • Accessors
      • get fix()
      • get id()
      • get link()
      • get message()
      • get problem()
      • get template()
    • Properties
      • static template
      • #fix
      • #id?
      • #link?
      • #problem
      • #template
    • Methods
      • static defineMessage()
      • static isError()
  • CommonErrors {}
    • Overview
    • Generic type variables
    • Constructor
    • Accessors
      • get errors()
    • Properties
      • static template?
      • #id?
      • #errors
    • Methods
      • delete()
      • has()
      • throw()
      • isAllowedId()
  • Error {}
    • Overview
    • Generic type variables
    • ★ Constructor
    • Accessors
      • get name()
      • get [Symbol.toStringTag]()
    • Methods
      • static define()
      • static isError()
  • Errors {}
    • Overview
    • Generic type variables
    • Constructor
    • Methods
      • get()
      • getErrors()
      • set()
    • Example usage
  • RangeError {}
    • Overview
    • Generic type variables
    • ★ Constructor
    • Accessors
      • get max()
      • get min()
      • get name()
      • get range()
      • get [Symbol.toStringTag]()
    • Properties
      • static template
      • #max?
      • #min?
    • Methods
      • static define()
      • static isRangeError()
  • RangeErrors {}
    • Overview
    • Generic type variables
    • Constructor
    • Methods
      • get()
      • getErrors()
      • set()
    • Example usage
  • TypeError {}
    • Overview
    • Generic type variables
    • ★ Constructor
    • Accessors
      • get name()
      • get type()
      • get [Symbol.toStringTag]()
    • Properties
      • static template
      • #type?
    • Methods
      • static define()
      • static isTypeError()
  • TypeErrors {}
    • Overview
    • Generic type variables
    • Constructor
    • Methods
      • get()
      • getErrors()
      • set()
  • ValidationError {}
    • Overview
    • Generic type variables
    • ★ Constructor
    • Accessors
      • get name()
      • get [Symbol.toStringTag]()
    • Methods
      • static define()
      • static isValidationError()
  • ValidationErrors {}
    • Overview
    • Generic type variables
    • Constructor
    • Methods
      • get()
      • getErrors()
      • set()
  • Change log
    • Keep a changelog
    • CHANGELOG.md
    • v3.0.0-rc
  • GIT
    • Commit
    • Semantic Versioning
  • License
    • MIT
  • Social
    • Gettr
    • Twitter
    • YouTube
  • Contact
    • ⋯ Chat
    • @ Email
    • ✆ Phone
  • Donate
    • ฿ Cryptocurrency
    • $ Fiat
Powered by GitBook
On this page
  • CommonError.defineMessage()
  • Parameters
  • Returns
  • Example usage

Was this helpful?

Edit on GitHub
  1. CommonError {}
  2. Methods

static defineMessage()

The static "tag" method builds from the given values the error message

PreviousMethodsNextstatic isError()

Last updated 3 years ago

Was this helpful?

CommonError.defineMessage()

The static "" method builds from the given the error message of a type on the template.

common-error.class.ts
protected static defineMessage(
  templateStringsArray: TemplateStringsArray,
  ...values: any[]
): string {
  let problem: string,
    fix: string,
    id: string | undefined,
    template: string,
    additional: { link?: string; min?: number; max?: number; type?: string };
  [problem, fix, id, template, additional] = values;
  template = (template || CommonError.template)
    .replace('{problem}', problem || '')
    .replace(/{id}/g, id || '')
    .replace(/{link}/g, additional?.link ? additional.link : '')
    .replace(/{max}/g, additional?.max ? String(additional.max) : '')
    .replace(/{min}/g, additional?.min ? String(additional.min) : '')
    .replace(/{type}/g, additional?.type ? additional.type : '')
    .replace('{fix}', fix || '');
  return template;
}

Parameters

templateStringsArray:TemplateStringsArray

-

Returns

Example usage

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

const problem = 'The given `age` parameter must be';
const fix = 'Provided string type is not accepted, change to ';
const id = 'AE: 427';
const template = `Issue({id}): {problem} of {type} between range {min} and {max}. {fix}{type}`;
const additional = { max: 27, min: 9, type: 'number' };

class TestError<Id extends string> extends CommonError<Id> {
  public static isError<Id extends string>(
    value: any,
    id?: Id
  ): value is CommonError<Id> {
    return super.isError(value, id);
  }

  public static defineMessage(
    templateStringsArray: TemplateStringsArray,
    ...values: any[]
  ): string {
    return super.defineMessage(templateStringsArray, ...values);
  }
}

// Returns
// Issue(AE: 427): The given `age` parameter must be of number between range 9 and 27. Provided string type is not accepted, change to number
TestError.defineMessage`${problem}${fix}${id}${template}${additional}`;

...values:[]

A rest parameter of expressions in order , , , and .

The return value is the error message of a type created from the expressions given in the .

any
tag
string
values
string
values
${problem}
${fix}
${id}
${template}
${additional}