Skip to content

Commit

Permalink
✨ feat: 细化支持设置、API 等错误类型
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 25, 2023
1 parent 0480e67 commit dad7c64
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export const PluginErrorType = {
PluginManifestNotFound: 'PluginManifestNotFound', // 插件描述文件不存在
PluginManifestInvalid: 'PluginManifestInvalid', // 插件描述文件不存在

PluginSettingsInvalid: 'PluginSettingsInvalid', // 插件设置不正确

PluginApiNotFound: 'PluginApiNotFound', // 插件 API 不存在
PluginApiParamsError: 'PluginApiParamsError', // 插件 API 请求入参有问题

// ******* 客户端错误 ******* //
BadRequest: 400,
Unauthorized: 401,
Expand All @@ -30,6 +35,7 @@ export type IPluginErrorType = (typeof PluginErrorType)[keyof typeof PluginError

const getStatus = (errorType: IPluginErrorType | string) => {
switch (errorType) {
case PluginErrorType.PluginApiNotFound:
case PluginErrorType.PluginMetaNotFound:
case PluginErrorType.PluginManifestNotFound:
return 404;
Expand All @@ -38,6 +44,8 @@ const getStatus = (errorType: IPluginErrorType | string) => {
return 490;
case PluginErrorType.PluginManifestInvalid:
return 491;
case PluginErrorType.PluginApiParamsError:
return 492;

case PluginErrorType.PluginMarketIndexNotFound:
return 590;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './error';
export * from './manifest';
export * from './market';
export * from './request';
export * from './types';
22 changes: 22 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const LOBE_PLUGIN_SETTINGS = 'X-LOBE_PLUGIN_SETTINGS';

export const getPluginSettingsStringFromRequest = <T = any>(req: Request): T | undefined => {
const settings = req.headers.get(LOBE_PLUGIN_SETTINGS);
if (!settings) return;

try {
return JSON.parse(settings);
} catch {
return settings as any;
}
};

export const createHeadersWithPluginSettings = (
settings: any,
header?: HeadersInit,

Check failure on line 16 in src/request.ts

View workflow job for this annotation

GitHub Actions / Test

'HeadersInit' is not defined

Check failure on line 16 in src/request.ts

View workflow job for this annotation

GitHub Actions / test

'HeadersInit' is not defined
): HeadersInit => {

Check failure on line 17 in src/request.ts

View workflow job for this annotation

GitHub Actions / Test

'HeadersInit' is not defined

Check failure on line 17 in src/request.ts

View workflow job for this annotation

GitHub Actions / test

'HeadersInit' is not defined
return {
...header,
[LOBE_PLUGIN_SETTINGS]: typeof settings === 'string' ? settings : JSON.stringify(settings),
};
};

0 comments on commit dad7c64

Please sign in to comment.