@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
62 lines (51 loc) • 1.74 kB
text/typescript
import { RESPONSES } from "./errorCodes";
/**
* This method takes an axios error response and parses it
* It pulls the most relevant data to be sent to xray
*/
export const getParsedResponse = (err): XRayEventData => {
const res = err?.response;
const req = err?.request;
const context = err?.context;
const name = err?.name;
const params = res?.config?.params || err?.config?.params;
const url = res?.config?.url || err?.config?.url;
const xRayData: XRayEventData = {
data: {
context,
name,
params,
url,
},
jsOnly: false,
};
if (res) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const statusCode = `${res?.data?.statusCode || res?.status}`;
const message = `Error: ${res?.data?.message || RESPONSES[statusCode]}`;
xRayData.message = message;
xRayData.data.statusCode = statusCode;
} else if (!res && req) {
// The request was made but no response was received
// `err.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
const message = `Error: ${err?.message || RESPONSES.noResponse}`;
xRayData.message = message;
} else {
// Something happened in setting up the request that triggered an Error
const message = `Error: ${err?.message || RESPONSES.unknown}`;
xRayData.message = message;
}
xRayData.exception = xRayData.message as unknown as Error; // TODO fix type
return xRayData;
};
export const invariant = (condition: boolean, errorDescription: string) => {
if (!__DEV__) {
// do nothing for production
return;
}
if (!condition) {
throw Error(errorDescription);
}
};