1// Similar interface to the one used in expo modules. 2type CodedError = Error & { code?: string }; 3 4let isErrorHandlingEnabled = true; 5 6const unavailableErrorPossibleSolutions = `Some possible solutions: 7- Make sure that the method is available on the current platform. 8- Make sure you're using the newest available version of this development client. 9- Make sure you're running a compatible version of your JavaScript code. 10- If you've installed a new library recently, you may need to make a new development client build.`; 11 12const moduleIsMissingPossibleSolutions = `Some possible solutions: 13- Make sure you're using the newest available version of this development client. 14- Make sure you're running a compatible version of your JavaScript code. 15- If you've installed a new library recently, you may need to make a new development client build.`; 16 17function customizeUnavailableMessage(error: CodedError) { 18 error.message += '\n\n' + unavailableErrorPossibleSolutions; 19} 20 21function customizeModuleIsMissingMessage(error: Error) { 22 error.message = `Your JavaScript code tried to access a native module that doesn't exist in this development client. 23 24${moduleIsMissingPossibleSolutions}`; 25} 26 27function customizeError(error: Error | CodedError) { 28 if ('code' in error) { 29 // It's a CodedError from expo modules 30 switch (error.code) { 31 case 'ERR_UNAVAILABLE': { 32 customizeUnavailableMessage(error); 33 break; 34 } 35 } 36 } else if (error.message.includes('Native module cannot be null')) { 37 customizeModuleIsMissingMessage(error); 38 } 39} 40 41function errorHandler(originalHandler, error, isFatal) { 42 if (error instanceof Error) { 43 customizeError(error); 44 } 45 46 originalHandler(error, isFatal); 47} 48 49export function createErrorHandler(originalHandler) { 50 return (error, isFatal) => { 51 if (isErrorHandlingEnabled) { 52 errorHandler(originalHandler, error, isFatal); 53 return; 54 } 55 56 originalHandler(error, isFatal); 57 }; 58} 59 60export function disableErrorHandling() { 61 isErrorHandlingEnabled = false; 62} 63