From eed435ececb6f8e7422ae014a0aa9ea8ed0113aa Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 15 Jun 2024 19:08:36 +0800 Subject: [PATCH] add error catcher --- packages/server/package.json | 2 +- packages/server/service/fetcher.ts | 17 +++++++++++++++-- packages/ui/build.ts | 1 - packages/ui/package.json | 20 ++++++++++---------- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/server/package.json b/packages/server/package.json index bf4e9c5..d567e69 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -25,7 +25,7 @@ "lodash": "^4.17.21", "nedb-promises": "^6.2.3", "schemastery": "^3.14.6", - "superagent": "^8.1.2", + "superagent": "^9.0.2", "unix-print": "^1.3.2", "ws": "^8.17.0" } diff --git a/packages/server/service/fetcher.ts b/packages/server/service/fetcher.ts index 79c8bb3..845b26c 100644 --- a/packages/server/service/fetcher.ts +++ b/packages/server/service/fetcher.ts @@ -5,8 +5,21 @@ import { config } from '../config'; import { Logger, mongoId } from '../utils'; const logger = new Logger('fetcher'); -const fetch = (url: string, type: 'get' | 'post' = 'get') => superagent[type](new URL(url, config.server).toString()) - .set('Authorization', config.token).set('Accept', 'application/json'); +const fetch = (url: string, type: 'get' | 'post' = 'get') => { + const endpoint = new URL(url, config.server).toString(); + const req = superagent[type](endpoint) + .set('Authorization', config.token) + .set('Accept', 'application/json'); + return new Proxy(req, { + get(target, prop) { + if (prop === 'then') { + return (...args) => target.then(...args) + .catch((e) => { throw new Error(`Failed to ${type} ${endpoint}: ${e.message}`); }); + } + return req[prop]; + }, + }); +}; export interface IBasicFetcher { contest: Record cron(): Promise diff --git a/packages/ui/build.ts b/packages/ui/build.ts index 533dd62..bae1acd 100644 --- a/packages/ui/build.ts +++ b/packages/ui/build.ts @@ -67,7 +67,6 @@ const compiler = webpack({ }, { test: /\.[mc]?[jt]sx?$/, - exclude: [/@types\//, /components\/message\//, /entry\.js/], type: 'javascript/auto', use: [esbuildLoader()], }, diff --git a/packages/ui/package.json b/packages/ui/package.json index a0e0c7a..0d5477f 100755 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -10,19 +10,19 @@ "preferUnplugged": true, "devDependencies": { "@hydrooj/xcpc-tools": "workspace:*", - "@mantine/code-highlight": "^7.10.1", - "@mantine/core": "^7.10.1", - "@mantine/form": "^7.10.1", - "@mantine/hooks": "^7.10.1", - "@mantine/modals": "^7.10.1", - "@mantine/notifications": "^7.10.1", - "@mantine/nprogress": "^7.10.1", + "@mantine/code-highlight": "^7.10.2", + "@mantine/core": "^7.10.2", + "@mantine/form": "^7.10.2", + "@mantine/hooks": "^7.10.2", + "@mantine/modals": "^7.10.2", + "@mantine/notifications": "^7.10.2", + "@mantine/nprogress": "^7.10.2", "@tabler/icons-react": "^2.47.0", - "@tanstack/react-query": "^5.44.0", + "@tanstack/react-query": "^5.45.0", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "css-loader": "^6.11.0", - "esbuild-loader": "^2.21.0", + "esbuild-loader": "^4.1.0", "mpegts.js": "^1.7.3", "postcss": "^8.4.38", "postcss-loader": "^7.3.3", @@ -33,7 +33,7 @@ "react-router-dom": "^6.23.1", "style-loader": "^3.3.4", "webpack": "^5.92.0", - "webpack-dev-server": "^4.15.2", + "webpack-dev-server": "^5.0.4", "webpackbar": "^6.0.1" } }