chore: 移除 cleanStack
parent
633c920247
commit
e643f36fe0
|
@ -23,13 +23,6 @@ export function captureError<T = any>(fn: () => void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkStack(error: Error) {
|
|
||||||
if (error.stack) {
|
|
||||||
return error.stack.indexOf('at') === error.stack.indexOf('at /');
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function noop() {
|
export function noop() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,19 +5,5 @@ export function assert(condition: boolean, msg: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function throwError(msg: string): void {
|
export function throwError(msg: string): void {
|
||||||
const error = new Error(`[axios-miniprogram]: ${msg}`);
|
throw new Error(`[axios-miniprogram]: ${msg}`);
|
||||||
cleanStack(error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function cleanStack(error: Error) {
|
|
||||||
const { stack } = error;
|
|
||||||
if (stack) {
|
|
||||||
const start = stack.indexOf('at');
|
|
||||||
const end = stack.search(/at ([\w-_.]+:)?\//i);
|
|
||||||
if (start < end) {
|
|
||||||
const removed = stack.slice(start, end);
|
|
||||||
error.stack = stack.replace(removed, '');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { cleanStack } from '../helpers/error';
|
|
||||||
import { AxiosAdapterPlatformTask } from '../adpater/createAdapter';
|
import { AxiosAdapterPlatformTask } from '../adpater/createAdapter';
|
||||||
import {
|
import {
|
||||||
AxiosRequestConfig,
|
AxiosRequestConfig,
|
||||||
|
@ -35,9 +34,7 @@ export function createError(
|
||||||
response: AxiosErrorResponse,
|
response: AxiosErrorResponse,
|
||||||
request: AxiosAdapterPlatformTask,
|
request: AxiosAdapterPlatformTask,
|
||||||
) {
|
) {
|
||||||
const axiosError = new AxiosError(message, config, response, request);
|
return new AxiosError(message, config, response, request);
|
||||||
cleanStack(axiosError);
|
|
||||||
return axiosError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAxiosError(value: unknown): value is AxiosError {
|
export function isAxiosError(value: unknown): value is AxiosError {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { captureError, checkStack } from 'scripts/test.utils';
|
import { assert, throwError } from '@/helpers/error';
|
||||||
import { assert, throwError, cleanStack } from '@/helpers/error';
|
|
||||||
|
|
||||||
describe('src/helpers/error.ts', () => {
|
describe('src/helpers/error.ts', () => {
|
||||||
test('第一个参数为 true 时应该无事发生', () => {
|
test('第一个参数为 true 时应该无事发生', () => {
|
||||||
|
@ -9,7 +8,6 @@ describe('src/helpers/error.ts', () => {
|
||||||
|
|
||||||
test('第一个参数为 false 时应该抛出异常', () => {
|
test('第一个参数为 false 时应该抛出异常', () => {
|
||||||
expect(() => assert(false, '')).toThrowError();
|
expect(() => assert(false, '')).toThrowError();
|
||||||
expect(checkStack(captureError(() => assert(false, '')))).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该抛出异常', () => {
|
test('应该抛出异常', () => {
|
||||||
|
@ -19,31 +17,5 @@ describe('src/helpers/error.ts', () => {
|
||||||
expect(() => throwError('error')).toThrowErrorMatchingInlineSnapshot(
|
expect(() => throwError('error')).toThrowErrorMatchingInlineSnapshot(
|
||||||
'"[axios-miniprogram]: error"',
|
'"[axios-miniprogram]: error"',
|
||||||
);
|
);
|
||||||
expect(checkStack(captureError(() => throwError('error')))).toBeTruthy();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('应该支持空错误栈', () => {
|
|
||||||
const ce = () => {
|
|
||||||
const error = new Error();
|
|
||||||
error.stack = undefined;
|
|
||||||
return error;
|
|
||||||
};
|
|
||||||
const error = ce();
|
|
||||||
|
|
||||||
cleanStack(error);
|
|
||||||
|
|
||||||
expect(checkStack(error)).toBeTruthy();
|
|
||||||
expect(error.stack).toBeUndefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('应该清掉多余的错误栈', () => {
|
|
||||||
const ce = () => new Error();
|
|
||||||
const error = ce();
|
|
||||||
|
|
||||||
expect(checkStack(error)).toBeFalsy();
|
|
||||||
|
|
||||||
cleanStack(error);
|
|
||||||
|
|
||||||
expect(checkStack(error)).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { checkStack } from 'scripts/test.utils';
|
|
||||||
import { createError, isAxiosError } from '@/request/createError';
|
import { createError, isAxiosError } from '@/request/createError';
|
||||||
|
|
||||||
describe('src/request/createError.ts', () => {
|
describe('src/request/createError.ts', () => {
|
||||||
|
@ -11,7 +10,6 @@ describe('src/request/createError.ts', () => {
|
||||||
expect(err.message).toBe('error');
|
expect(err.message).toBe('error');
|
||||||
expect(err.config).toBe(c);
|
expect(err.config).toBe(c);
|
||||||
expect(err.response).toBe(r);
|
expect(err.response).toBe(r);
|
||||||
expect(checkStack(err)).toBeTruthy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该支持传递更多参数', () => {
|
test('应该支持传递更多参数', () => {
|
||||||
|
|
Loading…
Reference in New Issue