import { ICustomError } from "./interface/ICustomError"; import { ErrorType } from "./enum/ErrorType"; const parseError = require('parse-error'); import { BaseError } from "./base/BaseError"; import { AgoraError } from "./AgoraError"; import { ErrorTitles } from "./enum/ErrorTitles"; export class CustomError extends BaseError { public type!: ErrorType; public title!: string; public message!: string; public datetime!: number; constructor(type: ErrorType, title : string, message: string){ super(type, title, message); } public static parseCustomError(error: any): Array{ let customError : Array = []; try { if(error instanceof CustomError){ //When error instance is create in code error.setType(ErrorType.GENERAL); customError.push(error); }else if(error && error.errors){ //when error result in fails validation rules if(error.errors){ let arrErrors = Object.values(error.errors); arrErrors.forEach((errorItem : any) => { if(errorItem.length > 0){ errorItem.forEach((err : any) => { if(err.length > 0){ customError.push(new CustomError(ErrorType.BUSINESS, ErrorTitles.BusinessRuleError, err)); } }); } }); } }else{ error = parseError(error); if(error.type && error.message){ //When error instance is create by try cacth in agora operations let agoraErrors : Array = AgoraError.parseCustomError(error); for (let index = 0; index < agoraErrors.length; index++) { const error = agoraErrors[index]; customError.push(error); } } } } catch (error) { customError.push(new CustomError(ErrorType.GENERAL, ErrorTitles.UnknownError, "unknown error")); } return customError; } }