axios-miniprogram/src/helpers/error.ts

23 lines
560 B
TypeScript
Raw Normal View History

2023-03-28 21:32:54 +08:00
export function assert(condition: boolean, msg: string) {
if (!condition) {
throwError(msg);
}
}
export function throwError(msg: string): void {
const error = new Error(`[axios-miniprogram]: ${msg}`);
cleanStack(error);
throw error;
}
export function cleanStack(error: Error) {
if (error.stack) {
const start = error.stack.indexOf('at');
2023-04-02 23:27:45 +08:00
const end = error.stack.search(/at ([\w-_.]+:)?\//i);
if (start < end) {
const removed = error.stack.slice(start, end);
error.stack = error.stack.replace(removed, '');
}
}
2023-03-28 21:32:54 +08:00
}