Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: error handling logic #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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