/** * An error to be used when an object of the same type already exists */ export class AlreadyExistsError extends Error { /** * An array of strings identifying the resources that caused the error * @private * @type { string[] } * @ignore */ private _conflicts: string[] = [] public constructor( message?: string, ...conflicts: string[] | AlreadyExistsError[] ) { super(message ? message : 'Already exists') this.name = 'AlreadyExistsError' Object.setPrototypeOf(this, AlreadyExistsError.prototype) if (conflicts.length > 0) { if (this.isStringArray(conflicts)) { // set string array to provided string array this._conflicts = conflicts as string[] } else { // set string array to content of string arrays from provided error array const arrays = (conflicts as AlreadyExistsError[]).map( item => item.conflicts ) this._conflicts = Array().concat(...arrays) } } } /** * Returns an array of strings identifying the resources that caused the error */ public get conflicts(): string[] { return this._conflicts } /** * Tests if an array is of type string[] * @param { [] } array The array to test */ private isStringArray(array: any[]): boolean { return Array.isArray(array) && array.every(item => typeof item === 'string') } }