fix: 配置接口补齐错误处理

This commit is contained in:
xream 2024-01-20 00:24:32 +08:00
parent c7d00ac512
commit 099ae5ad83
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
2 changed files with 45 additions and 21 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.185", "version": "2.14.186",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -1,5 +1,6 @@
import { SETTINGS_KEY, ARTIFACT_REPOSITORY_KEY } from '@/constants'; import { SETTINGS_KEY, ARTIFACT_REPOSITORY_KEY } from '@/constants';
import { success } from './response'; import { success, failed } from './response';
import { InternalServerError } from '@/restful/errors';
import $ from '@/core/app'; import $ from '@/core/app';
import Gist from '@/utils/gist'; import Gist from '@/utils/gist';
@ -10,6 +11,7 @@ export default function register($app) {
} }
async function getSettings(req, res) { async function getSettings(req, res) {
try {
let settings = $.read(SETTINGS_KEY); let settings = $.read(SETTINGS_KEY);
if (!settings) { if (!settings) {
settings = {}; settings = {};
@ -18,11 +20,23 @@ async function getSettings(req, res) {
if (!settings.avatarUrl) await updateGitHubAvatar(); if (!settings.avatarUrl) await updateGitHubAvatar();
if (!settings.artifactStore) await updateArtifactStore(); if (!settings.artifactStore) await updateArtifactStore();
success(res, settings); success(res, settings);
// TODO: 缺错误处理 前端也缺 } catch (e) {
$.error(`Failed to get settings: ${e.message ?? e}`);
failed(
res,
new InternalServerError(
`FAILED_TO_GET_SETTINGS`,
`Failed to get settings`,
`Reason: ${e.message ?? e}`,
),
);
}
} }
async function updateSettings(req, res) { async function updateSettings(req, res) {
try {
const settings = $.read(SETTINGS_KEY); const settings = $.read(SETTINGS_KEY);
const newSettings = { const newSettings = {
...settings, ...settings,
@ -32,7 +46,17 @@ async function updateSettings(req, res) {
await updateGitHubAvatar(); await updateGitHubAvatar();
await updateArtifactStore(); await updateArtifactStore();
success(res, newSettings); success(res, newSettings);
// TODO: 缺错误处理 前端也缺 } catch (e) {
$.error(`Failed to update settings: ${e.message ?? e}`);
failed(
res,
new InternalServerError(
`FAILED_TO_UPDATE_SETTINGS`,
`Failed to update settings`,
`Reason: ${e.message ?? e}`,
),
);
}
} }
export async function updateGitHubAvatar() { export async function updateGitHubAvatar() {