@embrace-io/react-native
Version:
A React Native wrapper for the Embrace SDK
60 lines (59 loc) • 2.72 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleError = exports.handleGlobalError = void 0;
const react_native_1 = require("react-native");
const component_1 = require("./component");
const EmbraceManagerModule_1 = require("./../EmbraceManagerModule");
const STACK_LIMIT = 200;
const handleGlobalError = (previousHandler, handleError) => (error, isFatal) => {
const callback = () => {
setTimeout(() => {
previousHandler(error, isFatal);
}, 150);
};
handleError(error, callback);
};
exports.handleGlobalError = handleGlobalError;
// will cover unhandled errors, js crashes
const handleError = (error, callback) => __awaiter(void 0, void 0, void 0, function* () {
if (!(error instanceof Error)) {
console.warn("[Embrace] error must be of type Error");
return;
}
const { name, message, stack = "" } = error;
(0, component_1.logIfComponentError)(error);
// same as error.name? why is it pulled differently?
const errorType = error.constructor.name;
// truncating stacktrace to 200 lines
const stTruncated = stack.split("\n").slice(0, STACK_LIMIT);
// specifically for iOS for now, the same formatting is done in the Android layer
// in the future Android will get rid of all related to js and use this format as well
const iosStackTrace = JSON.stringify({
n: name,
m: message,
t: errorType,
// removing the Type from the first part of the stacktrace.
st: stTruncated.slice(1, stTruncated.length).join("\n"),
});
let logException;
try {
logException = yield EmbraceManagerModule_1.EmbraceManagerModule.logUnhandledJSException(name, message, errorType, react_native_1.Platform.OS === "android" ? stTruncated.join("\n") : iosStackTrace);
}
catch (_a) {
logException = false;
}
if (!logException) {
console.warn("[Embrace] Failed to log exception");
}
callback();
});
exports.handleError = handleError;