floating-live 0.16.0-alpha.15 → 0.16.0-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { AppError } from "../error";
1
2
  export class CommandManager {
2
3
  constructor(app) {
3
4
  /** 功能列表 */
@@ -23,10 +24,10 @@ export class CommandManager {
23
24
  callWithOptions(name, options, ...args) {
24
25
  const command = this.list.get(name);
25
26
  if (!command) {
26
- throw {
27
- message: "命令不存在",
28
- id: "command:call_unexist",
29
- };
27
+ throw new AppError("command:call_unexist", {
28
+ message: `命令不存在: ${name}`,
29
+ target: `command/${name}`,
30
+ });
30
31
  }
31
32
  else {
32
33
  return command.call({ source: options.source, client: options.client }, ...args);
@@ -1,10 +1,12 @@
1
1
  import { ErrorOptions } from "./types";
2
2
  export * from "./types";
3
3
  export declare class AppError extends Error {
4
- /** 错误来源 */
5
- source?: string;
6
4
  /** 错误id */
7
5
  id: string;
6
+ /** 错误来源 */
7
+ source?: string;
8
+ /** 错误目标 */
9
+ target?: string;
8
10
  /** 错误原因 */
9
11
  cause?: Error | string;
10
12
  constructor(id: string, options: ErrorOptions);
@@ -2,5 +2,6 @@ export interface ErrorOptions {
2
2
  message?: string;
3
3
  cause?: string | Error;
4
4
  source?: string;
5
+ target?: string;
5
6
  [key: string]: any;
6
7
  }
@@ -1,13 +1,5 @@
1
1
  import { App } from "../app";
2
- import { AppError, ErrorOptions } from "../error";
3
2
  import { HookFunction, HookUseOptions } from "./types";
4
- export interface HookErrorOptions extends ErrorOptions {
5
- hookName: string;
6
- }
7
- export declare class HookError extends AppError {
8
- hookName: string;
9
- constructor(id: string, options: HookErrorOptions);
10
- }
11
3
  export declare class HookManager {
12
4
  private list;
13
5
  private pluginMap;
@@ -1,10 +1,4 @@
1
1
  import { AppError } from "../error";
2
- export class HookError extends AppError {
3
- constructor(id, options) {
4
- super(id, options);
5
- this.hookName = options.hookName;
6
- }
7
- }
8
2
  export class HookManager {
9
3
  constructor(app) {
10
4
  this.list = new Map();
@@ -20,10 +14,10 @@ export class HookManager {
20
14
  this.list.set(name, hooks);
21
15
  }
22
16
  else if (hooks.includes(call)) {
23
- throw new HookError("hook:use_duplicate", {
17
+ throw new AppError("hook:use_duplicate", {
24
18
  message: `钩子函数挂载失败: ${name}`,
25
19
  cause: "重复挂载同一函数",
26
- hookName: name,
20
+ target: `hook/${name}`,
27
21
  });
28
22
  }
29
23
  hooks.push({ call, pluginName });
@@ -31,15 +25,13 @@ export class HookManager {
31
25
  /** 取消挂载钩子函数 */
32
26
  unuse(name, call) {
33
27
  let hooks = this.list.get(name);
34
- if (!hooks)
35
- throw new HookError("hook:unuse_unexisted", {
28
+ const index = hooks?.findIndex((item) => item.call == call) ?? -1;
29
+ if (!hooks || index < 0)
30
+ throw new AppError("hook:unuse_unexist", {
36
31
  message: `钩子函数卸载失败: ${name}`,
37
32
  cause: "卸载不存在的钩子函数",
38
- hookName: name,
33
+ target: `hook/${name}`,
39
34
  });
40
- const index = hooks.findIndex((item) => item.call == call);
41
- if (index < 0)
42
- throw {};
43
35
  hooks.splice(index, 1);
44
36
  }
45
37
  /** 调用钩子函数 */
@@ -1,8 +1,11 @@
1
+ import { AppError } from "../error";
1
2
  import type { PluginItem, PluginContext } from "./types";
2
3
  export type * from "./types";
3
4
  export declare class BasePlugin implements PluginItem {
4
5
  static pluginName: string;
5
6
  pluginName: string;
6
7
  ctx: PluginContext;
8
+ readonly Error: typeof AppError;
9
+ readonly throw: (err?: Error) => void;
7
10
  constructor(ctx: PluginContext, options?: any);
8
11
  }
@@ -1,5 +1,8 @@
1
+ import { AppError } from "../error";
1
2
  export class BasePlugin {
2
3
  constructor(ctx, options) {
4
+ this.Error = AppError;
3
5
  this.ctx = ctx;
6
+ this.throw = ctx.throw;
4
7
  }
5
8
  }
@@ -1,13 +1,5 @@
1
1
  import type { App } from "../app";
2
- import { AppError, ErrorOptions } from "../error";
3
2
  import { PluginItem, PluginConstructor, PluginContext, PluginRegisterOptions } from "./types";
4
- export interface PluginErrorOptions extends ErrorOptions {
5
- pluginName: string;
6
- }
7
- export declare class PluginError extends AppError {
8
- pluginName: string;
9
- constructor(id: string, options: PluginErrorOptions);
10
- }
11
3
  export interface PluginData<T> {
12
4
  plugin: PluginItem;
13
5
  context: PluginContext;
@@ -1,12 +1,6 @@
1
1
  import { CommonPluginContext } from "./context/CommonPluginContext";
2
2
  import { AppError } from "../error";
3
3
  import { CorePluginContext } from "./context/CorePluginContext";
4
- export class PluginError extends AppError {
5
- constructor(id, options) {
6
- super(id, options);
7
- this.pluginName = options.pluginName;
8
- }
9
- }
10
4
  export class PluginManager {
11
5
  constructor(app) {
12
6
  /** 插件列表 */
@@ -16,17 +10,17 @@ export class PluginManager {
16
10
  registerSync(pluginConstructor, options = {}, { context, core } = {}) {
17
11
  const pluginName = pluginConstructor.pluginName;
18
12
  if (!pluginName) {
19
- throw new PluginError("plugin:register_id_missing", {
13
+ throw new AppError("plugin:register_id_missing", {
20
14
  message: "插件注册失败",
21
15
  cause: "插件缺少pluginName字段",
22
- pluginName: "",
16
+ target: "plugin/#unnamed",
23
17
  });
24
18
  }
25
19
  if (this.list.has(pluginName)) {
26
- throw new PluginError("plugin:register_id_duplicate", {
20
+ throw new AppError("plugin:register_id_duplicate", {
27
21
  message: `插件注册失败: ${pluginName}`,
28
22
  cause: "已存在相同id的插件",
29
- pluginName,
23
+ target: `plugin/${pluginName}`,
30
24
  });
31
25
  }
32
26
  const registerCtx = { pluginName, options };
@@ -34,10 +28,10 @@ export class PluginManager {
34
28
  try {
35
29
  const ctx = this.app.callHookSync("plugin.register", registerCtx);
36
30
  if (ctx.defaultPrevented)
37
- throw new PluginError("plugin:register_hook_prevented", {
31
+ throw new AppError("plugin:register_hook_prevented", {
38
32
  message: `插件注册失败: ${pluginName}`,
39
33
  cause: "插件注册被钩子函数阻止",
40
- pluginName,
34
+ target: `plugin/${pluginName}`,
41
35
  });
42
36
  // 执行插件函数
43
37
  const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
@@ -55,10 +49,10 @@ export class PluginManager {
55
49
  return plugin;
56
50
  }
57
51
  catch (err) {
58
- throw new PluginError("plugin:register_fail", {
52
+ throw new AppError("plugin:register_fail", {
59
53
  message: `插件注册失败: ${pluginName}`,
60
54
  cause: err,
61
- pluginName,
55
+ target: `plugin/${pluginName}`,
62
56
  });
63
57
  }
64
58
  }
@@ -66,17 +60,17 @@ export class PluginManager {
66
60
  async register(pluginConstructor, options = {}, { context, core } = {}) {
67
61
  const pluginName = pluginConstructor.pluginName;
68
62
  if (!pluginName) {
69
- throw new PluginError("plugin:register_id_missing", {
63
+ throw new AppError("plugin:register_id_missing", {
70
64
  message: "插件注册失败",
71
65
  cause: "插件缺少pluginName字段",
72
- pluginName: "",
66
+ target: "plugin/#unnamed",
73
67
  });
74
68
  }
75
69
  if (this.list.has(pluginName)) {
76
- throw new PluginError("plugin:register_id_duplicate", {
70
+ throw new AppError("plugin:register_id_duplicate", {
77
71
  message: `插件注册失败: ${pluginName}`,
78
72
  cause: "已存在相同id的插件",
79
- pluginName,
73
+ target: `plugin/${pluginName}`,
80
74
  });
81
75
  }
82
76
  const registerCtx = { pluginName, options };
@@ -85,10 +79,10 @@ export class PluginManager {
85
79
  .callHook("plugin.register", registerCtx)
86
80
  .then(async (ctx) => {
87
81
  if (ctx.defaultPrevented)
88
- throw new PluginError("plugin:register_hook_prevented", {
82
+ throw new AppError("plugin:register_hook_prevented", {
89
83
  message: `插件注册失败: ${pluginName}`,
90
84
  cause: "插件注册被钩子函数阻止",
91
- pluginName,
85
+ target: `plugin/${pluginName}`,
92
86
  });
93
87
  // 执行插件函数
94
88
  const plugin = new pluginConstructor(pluginCtx, registerCtx.options || {});
@@ -106,10 +100,10 @@ export class PluginManager {
106
100
  return plugin;
107
101
  })
108
102
  .catch((err) => {
109
- throw new PluginError("plugin:register_fail", {
103
+ throw new AppError("plugin:register_fail", {
110
104
  message: `插件注册失败: ${pluginName}`,
111
105
  cause: err,
112
- pluginName,
106
+ target: `plugin/${pluginName}`,
113
107
  });
114
108
  });
115
109
  }
@@ -118,19 +112,19 @@ export class PluginManager {
118
112
  const pluginData = this.list.get(pluginName);
119
113
  // 检测插件是否存在
120
114
  if (!pluginData) {
121
- throw new PluginError("plugin:unregister_unexist", {
115
+ throw new AppError("plugin:unregister_unexist", {
122
116
  message: `插件移除失败: ${pluginName}`,
123
117
  cause: "插件不存在",
124
- pluginName,
118
+ target: `plugin/${pluginName}`,
125
119
  });
126
120
  }
127
121
  else {
128
122
  const { plugin, context, core } = pluginData;
129
123
  if (core) {
130
- throw new PluginError("plugin:unregister_core", {
124
+ throw new AppError("plugin:unregister_core", {
131
125
  message: `插件移除失败: ${pluginName}`,
132
126
  cause: "无法移除核心插件",
133
- pluginName,
127
+ target: `plugin/${pluginName}`,
134
128
  });
135
129
  }
136
130
  // 调用插件和上下文的destroy钩子
@@ -45,7 +45,7 @@ export interface PluginContext {
45
45
  emit<K extends keyof AppEventDetailMap>(type: K, detail: AppEventDetailMap[K], options?: AppEventEmitOptions & EventInit): void;
46
46
  /** 抛出错误 */
47
47
  throw(err?: Error): void;
48
- /** 生成错误 */
48
+ /** 生成错误 @deprecated */
49
49
  error(id: string, options: ErrorOptions): AppError;
50
50
  /** 注册插件 */
51
51
  register<P extends PluginItem>(plugin: PluginConstructor<P>, options?: PluginInitOptions): Promise<P>;
@@ -32,7 +32,7 @@ export class Room extends BasePlugin {
32
32
  attach(room, open) {
33
33
  let key = room.key;
34
34
  if (this.map.has(key)) {
35
- this.ctx.throw(new AppError("room:add_exist", {
35
+ this.throw(new AppError("room:add_exist", {
36
36
  message: "房间已存在",
37
37
  }));
38
38
  return;
@@ -108,25 +108,32 @@ export class Room extends BasePlugin {
108
108
  const ctx = { platform, id, options: opt };
109
109
  const res = await this.ctx.callHook("room.add", ctx);
110
110
  if (res.defaultPrevented) {
111
- this.ctx.throw(this.ctx.error("room:add_hook_prevented", {
111
+ this.throw(new this.Error("room:add_hook_prevented", {
112
112
  message: "无法添加房间",
113
113
  cause: "房间添加被钩子函数阻止",
114
+ target: `room/${platform}:${id}`,
114
115
  }));
115
116
  }
116
- const room = await this.ctx.call(`${platform}.room.create`, id, opt);
117
- if (!room) {
118
- this.ctx.throw(this.ctx.error("room:create_fail", { message: "无法创建房间" }));
117
+ let room;
118
+ try {
119
+ room = await this.ctx.call(`${platform}.room.create`, id, opt);
119
120
  }
120
- else {
121
- this.attach(room);
121
+ catch (e) {
122
+ this.throw(new this.Error("room:create_fail", {
123
+ message: "无法创建房间",
124
+ cause: e,
125
+ target: `room/${platform}:${id}`,
126
+ }));
122
127
  }
128
+ this.attach(room);
123
129
  }
124
130
  /** 移除房间 */
125
131
  remove(key) {
126
132
  let room = this.map.get(key);
127
133
  if (!room) {
128
- this.ctx.throw(this.ctx.error("room:remove_unexist", {
134
+ this.throw(new this.Error("room:remove_unexist", {
129
135
  message: "房间不存在",
136
+ target: `room/${key}`,
130
137
  }));
131
138
  }
132
139
  this.map.delete(key); // 从表中删除房间
@@ -1,3 +1,4 @@
1
+ import { AppError } from "../error";
1
2
  import { bindCommand } from "../utils";
2
3
  export class ValueManager {
3
4
  constructor(app) {
@@ -25,7 +26,9 @@ export class ValueManager {
25
26
  get(name, options) {
26
27
  const item = this.list.get(name);
27
28
  if (!item)
28
- throw {};
29
+ throw new AppError("value:get_unexist", {
30
+ message: "无法获取未注册的值",
31
+ });
29
32
  return item.get(options);
30
33
  }
31
34
  /** 获取值(不存在则返回undefined) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floating-live",
3
- "version": "0.16.0-alpha.15",
3
+ "version": "0.16.0-alpha.16",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",