import msgHandler from '../utils/iframe/messageHandler'; export enum MiniAppErrorType { invalidUrl = 'INVALID_URL', invalidData = 'INVALID_DATA', invalidHeader = 'INVALID_HEADER', invalidMethod = 'INVALID_METHOD', invalidPath = 'INVALID_PATH', notReachable = 'NOT_REACHABLE', timeOut = 'TIME_OUT', whiteListError = 'NONE_WHITELIST_DOMAIN', statusCodeError = 'STATUS_CODE_ERROR', invalidResponse = 'INVALID_RESPONSE', noLocationPermission = 'NO_USER_LOCATION_PERMISSION', noPermissionListAvailable = 'NO_PERMISSION_LIST_AVAILABLE', other = 'other', serverError = 'SERVER_ERROR', noCameraPermission = 'HOST_APP_CAMERA_DENIED', noAlbumPermission = 'HOST_APP_ALBUM_DENIED', insufficientScope = 'INSUFFICIENT_SCOPE', userCanceled = 'USER_CANCELED', photoError = 'PHOTO_ERROR', invalidJSAPIParams = 'INVALID_JS_API_PARAMS', hostAppLocationDenied = 'HOST_APP_LOCATION_DENIED', hostAppContactsDenied = 'HOST_APP_READING_CONTACT_DENIED', badRequestInsufficientParameter = 'BAD_REQUEST_INSUFFICIENT_PARAMETER', paymentFail = 'PAYMENT_FAIL', topupFail = 'TOPUP_FAIL', topupSuccessButNotAddToBalance = 'TOUP_SUCCESS_BUT_NOT_ADD_TO_BALANCE', userCanceledSimilarTxn = 'USER_CANCELED_SIMILAR_TRANSACTION', mapAppNotFound = 'MAP_APP_NOT_FOUND', invalidAppSchemeURL = 'INVALID_APP_SCHEME_URL', noValueFound = 'NO_VALUE_FOUND', securityNotSetup = 'SECURITY_NOT_SETUP', userDenied = 'USER_DENIED', securityTemporaryLocked = 'SECURITY_TEMPORARY_LOCKED', couldNotGenerateBarcode = 'COULD_NOT_GENERATE_BARCODE', cannotDetectAvailability = 'CAN_NOT_DETECT_AVAILABILITY', unknown = 'UNKNOWN', } export class MiniAppError extends Error { constructor(type: MiniAppErrorType) { super(type); this.message = type; } toString() { switch (this.message) { case MiniAppErrorType.invalidUrl: return 'URL is invalid'; case MiniAppErrorType.invalidData: return 'Data is invalid'; case MiniAppErrorType.invalidHeader: return 'Header is invalid'; case MiniAppErrorType.invalidMethod: return 'Method is invalid'; case MiniAppErrorType.notReachable: return 'Device not connected to internet'; case MiniAppErrorType.timeOut: return 'Request time out'; case MiniAppErrorType.whiteListError: return "Remote server's domain is not registered in white list inside app.json"; case MiniAppErrorType.statusCodeError: return 'Received non 20X status code'; case MiniAppErrorType.invalidResponse: return 'Cannot stringfy response data'; default: return this.message; } } static handleError(error: Error, data: any) { if (error instanceof MiniAppError) { msgHandler(data?.originId, data?.failCB, { errorCode: error.message, }); } else { msgHandler(data?.originId, data?.failCB, { errorCode: MiniAppErrorType.serverError, }); } } }