Skip to content

Commit

Permalink
refactor: error handling logic
Browse files Browse the repository at this point in the history
- Introduce `UnError` class to unify error handling
- Replace existing error handling methods with `UnError` to encapsulate error information
- Optimize code structure to improve consistency and readability of error handling
  • Loading branch information
peerless-hero committed Dec 1, 2024
1 parent 1146aa3 commit 4c70023
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 59 deletions.
31 changes: 13 additions & 18 deletions packages/core/src/adapters/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import statuses from "statuses-es";
import type { UnCancelTokenListener } from "../core/UnCancelToken";
import { UnCanceledError } from "../core/UnCanceledError";
import { UnError } from "../core/UnError";
import { settle } from "../core/settle";
import type { UnConfig, UnData, UnResponse } from "../types";
import { buildDownloadConfig } from "../utils";
Expand All @@ -26,7 +27,6 @@ export const downloadAdapter = <T = UnData, D = UnData>(
signal?.removeEventListener("abort", onCanceled);
};

let response: UnResponse<T, D>;
let task: UniApp.DownloadTask | undefined;

task = uni.downloadFile({
Expand All @@ -40,8 +40,7 @@ export const downloadAdapter = <T = UnData, D = UnData>(
// 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined
statusText = undefined;
}
response = {
...response,
const response: UnResponse<T, D> = {
// @ts-expect-error no types
errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message,
// @ts-expect-error no types
Expand All @@ -61,21 +60,6 @@ export const downloadAdapter = <T = UnData, D = UnData>(
},
task,
};
},
fail: (err) => {
response = {
...response,
errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message,
errno: err?.errno,
};
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onProgressUpdate) {
task?.offProgressUpdate(onProgressUpdate);
}
settle<T, D, UnResponse<T, D>>(
(val) => {
resolve(val);
Expand All @@ -88,6 +72,17 @@ export const downloadAdapter = <T = UnData, D = UnData>(
response,
);
},
fail: (err) => {
reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task));
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onProgressUpdate) {
task?.offProgressUpdate(onProgressUpdate);
}
},
});

if (onHeadersReceived) {
Expand Down
35 changes: 14 additions & 21 deletions packages/core/src/adapters/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import statuses from "statuses-es";
import type { UnCancelTokenListener } from "../core/UnCancelToken";
import { UnCanceledError } from "../core/UnCanceledError";
import { UnError } from "../core/UnError";
import { settle } from "../core/settle";
import type { UnConfig, UnData, UnResponse } from "../types";
import { buildRequestConfig } from "../utils";
Expand All @@ -19,7 +20,6 @@ export const requestAdapter = <T = UnData, D = UnData>(
signal?.removeEventListener?.("abort", onCanceled);
};

let response: UnResponse<T, D>;
let task: UniApp.RequestTask | undefined;

task = uni.request({
Expand All @@ -33,8 +33,7 @@ export const requestAdapter = <T = UnData, D = UnData>(
// 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined
statusText = undefined;
}
response = {
...response,
const response: UnResponse<T, D> = {
// @ts-expect-error no types
errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message,
// @ts-expect-error no types
Expand All @@ -50,24 +49,6 @@ export const requestAdapter = <T = UnData, D = UnData>(
data: res?.data,
task,
};
},
fail: (err) => {
response = {
...response,
// @ts-expect-error no types
errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message,
// @ts-expect-error no types
errno: err?.errno,
};
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onChunkReceived) {
// @ts-expect-error uni-app types lost
task?.offChunkReceived(onChunkReceived);
}
settle<T, D, UnResponse<T, D>>(
(val) => {
resolve(val);
Expand All @@ -80,6 +61,18 @@ export const requestAdapter = <T = UnData, D = UnData>(
response,
);
},
fail: (err) => {
reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task));
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onChunkReceived) {
// @ts-expect-error uni-app types lost
task?.offChunkReceived(onChunkReceived);
}
},
});

if (onHeadersReceived) {
Expand Down
33 changes: 13 additions & 20 deletions packages/core/src/adapters/upload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import statuses from "statuses-es";
import type { UnCancelTokenListener } from "../core/UnCancelToken";
import { UnCanceledError } from "../core/UnCanceledError";
import { UnError } from "../core/UnError";
import { settle } from "../core/settle";
import type { UnConfig, UnData, UnResponse } from "../types";
import { buildUploadConfig } from "../utils";
Expand All @@ -24,7 +25,6 @@ export const uploadAdapter = <T = UnData, D = UnData>(config: UnConfig<T, D>) =>
signal?.removeEventListener("abort", onCanceled);
};

let response: UnResponse<T, D>;
let task: UniApp.UploadTask | undefined;

task = uni.uploadFile({
Expand All @@ -38,8 +38,7 @@ export const uploadAdapter = <T = UnData, D = UnData>(config: UnConfig<T, D>) =>
// 当 statusCode 不合法、statuses 抛出错误时,设置 statusText 为 undefined
statusText = undefined;
}
response = {
...response,
const response: UnResponse<T, D> = {
// @ts-expect-error no types
errMsg: res?.errMsg ?? res?.errmsg ?? res?.msg ?? res?.message,
// @ts-expect-error no types
Expand All @@ -53,23 +52,6 @@ export const uploadAdapter = <T = UnData, D = UnData>(config: UnConfig<T, D>) =>
data: res?.data,
task,
};
},
fail: (err) => {
response = {
...response,
// @ts-expect-error no types
errMsg: err?.errMsg ?? err?.errmsg ?? err?.msg ?? err?.message,
// @ts-expect-error no types
errno: err?.errno,
};
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onProgressUpdate) {
task?.offProgressUpdate(onProgressUpdate);
}
settle<T, D, UnResponse<T, D>>(
(val) => {
resolve(val);
Expand All @@ -82,6 +64,17 @@ export const uploadAdapter = <T = UnData, D = UnData>(config: UnConfig<T, D>) =>
response,
);
},
fail: (err) => {
reject(new UnError(err.errMsg, UnError.ERR_NETWORK, config, task));
},
complete: () => {
if (onHeadersReceived) {
task?.offHeadersReceived(onHeadersReceived);
}
if (onProgressUpdate) {
task?.offProgressUpdate(onProgressUpdate);
}
},
});

if (onHeadersReceived) {
Expand Down

0 comments on commit 4c70023

Please sign in to comment.