UNPKG

react-native-ios-alarmkit

Version:
49 lines (48 loc) 1.48 kB
export const AlarmKitErrorCode = { INVALID_UUID: 'INVALID_UUID', INVALID_JSON: 'INVALID_JSON', INVALID_CONFIGURATION: 'INVALID_CONFIGURATION', ALARM_NOT_FOUND: 'ALARM_NOT_FOUND', MAXIMUM_LIMIT_REACHED: 'MAXIMUM_LIMIT_REACHED', UNAUTHORIZED: 'UNAUTHORIZED', ALARM_EXISTS: 'ALARM_EXISTS', UNKNOWN: 'UNKNOWN', }; export class AlarmKitError extends Error { code; domain; nativeCode; nativeError; constructor(info, nativeError) { super(info.message); this.name = 'AlarmKitError'; this.code = info.code; this.domain = info.domain; this.nativeCode = info.nativeCode; this.nativeError = nativeError; Object.setPrototypeOf(this, AlarmKitError.prototype); } static fromError(error) { if (error instanceof AlarmKitError) { return error; } const errorString = String(error); try { const jsonMatch = errorString.match(/\{.*\}/); if (jsonMatch) { const errorInfo = JSON.parse(jsonMatch[0]); return new AlarmKitError(errorInfo, errorString); } } catch { // Failed to parse, fall through to default } return new AlarmKitError({ code: AlarmKitErrorCode.UNKNOWN, message: errorString, }, errorString); } toString() { return `AlarmKitError [${this.code}]: ${this.message}`; } }