@codifycli/plugin-core 1.1.0-beta17 → 1.1.0-beta19

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,6 +1,7 @@
1
1
  import { ApplyRequestDataSchema, EmptyResponseDataSchema, GetResourceInfoRequestDataSchema, GetResourceInfoResponseDataSchema, ImportRequestDataSchema, ImportResponseDataSchema, InitializeRequestDataSchema, InitializeResponseDataSchema, IpcMessageSchema, IpcMessageV2Schema, MatchRequestDataSchema, MatchResponseDataSchema, MessageStatus, PlanRequestDataSchema, PlanResponseDataSchema, ResourceSchema, SetVerbosityRequestDataSchema, ValidateRequestDataSchema, ValidateResponseDataSchema } from '@codifycli/schemas';
2
2
  import { Ajv } from 'ajv';
3
3
  import addFormats from 'ajv-formats';
4
+ import { ApplyValidationError } from '../common/errors.js';
4
5
  import { SudoError } from '../errors.js';
5
6
  const SupportedRequests = {
6
7
  'initialize': {
@@ -113,22 +114,30 @@ export class MessageHandler {
113
114
  }
114
115
  // @ts-expect-error TS2239
115
116
  const cmd = message.cmd + '_Response';
117
+ // @ts-expect-error TS2239
118
+ const requestId = message.requestId || undefined;
119
+ let errorPayload;
116
120
  if (e instanceof SudoError) {
117
- return process.send?.({
118
- cmd,
119
- // @ts-expect-error TS2239
120
- requestId: message.requestId || undefined,
121
- data: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
122
- status: MessageStatus.ERROR,
123
- });
121
+ errorPayload = {
122
+ errorType: 'sudo_error',
123
+ message: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
124
+ data: { command: e.command, pluginName: this.plugin.name },
125
+ };
126
+ }
127
+ else if (e instanceof ApplyValidationError) {
128
+ errorPayload = {
129
+ errorType: 'apply_validation',
130
+ message: e.message,
131
+ data: { plan: e.plan.toResponse() },
132
+ };
133
+ }
134
+ else {
135
+ const isDebug = process.env.DEBUG?.includes('*') ?? false;
136
+ errorPayload = {
137
+ errorType: 'unknown',
138
+ message: isDebug ? (e.stack ?? e.message) : e.message,
139
+ };
124
140
  }
125
- const isDebug = process.env.DEBUG?.includes('*') ?? false;
126
- process.send?.({
127
- cmd,
128
- // @ts-expect-error TS2239
129
- requestId: message.requestId || undefined,
130
- data: isDebug ? e.stack : e.message,
131
- status: MessageStatus.ERROR,
132
- });
141
+ process.send?.({ cmd, requestId, data: errorPayload, status: MessageStatus.ERROR });
133
142
  }
134
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codifycli/plugin-core",
3
- "version": "1.1.0-beta17",
3
+ "version": "1.1.0-beta19",
4
4
  "description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "license": "ISC",
37
37
  "dependencies": {
38
- "@codifycli/schemas": "1.1.0-beta6",
38
+ "@codifycli/schemas": "^1.1.0-beta8",
39
39
  "@homebridge/node-pty-prebuilt-multiarch": "^0.13.1",
40
40
  "ajv": "^8.18.0",
41
41
  "ajv-formats": "^2.1.1",
@@ -14,6 +14,7 @@ import {
14
14
  MatchRequestDataSchema,
15
15
  MatchResponseDataSchema,
16
16
  MessageStatus,
17
+ PluginErrorData,
17
18
  PlanRequestDataSchema,
18
19
  PlanResponseDataSchema,
19
20
  ResourceSchema,
@@ -24,6 +25,7 @@ import {
24
25
  import { Ajv, SchemaObject, ValidateFunction } from 'ajv';
25
26
  import addFormats from 'ajv-formats';
26
27
 
28
+ import { ApplyValidationError } from '../common/errors.js';
27
29
  import { SudoError } from '../errors.js';
28
30
  import { Plugin } from '../plugin/plugin.js';
29
31
 
@@ -157,25 +159,31 @@ export class MessageHandler {
157
159
 
158
160
  // @ts-expect-error TS2239
159
161
  const cmd = message.cmd + '_Response';
162
+ // @ts-expect-error TS2239
163
+ const requestId = message.requestId || undefined;
164
+
165
+ let errorPayload: PluginErrorData;
160
166
 
161
167
  if (e instanceof SudoError) {
162
- return process.send?.({
163
- cmd,
164
- // @ts-expect-error TS2239
165
- requestId: message.requestId || undefined,
166
- data: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
167
- status: MessageStatus.ERROR,
168
- })
168
+ errorPayload = {
169
+ errorType: 'sudo_error',
170
+ message: `Plugin: '${this.plugin.name}'. Forbidden usage of sudo for command '${e.command}'. Please contact the plugin developer to fix this.`,
171
+ data: { command: e.command, pluginName: this.plugin.name },
172
+ };
173
+ } else if (e instanceof ApplyValidationError) {
174
+ errorPayload = {
175
+ errorType: 'apply_validation',
176
+ message: e.message,
177
+ data: { plan: e.plan.toResponse() },
178
+ };
179
+ } else {
180
+ const isDebug = process.env.DEBUG?.includes('*') ?? false;
181
+ errorPayload = {
182
+ errorType: 'unknown',
183
+ message: isDebug ? (e.stack ?? e.message) : e.message,
184
+ };
169
185
  }
170
186
 
171
- const isDebug = process.env.DEBUG?.includes('*') ?? false;
172
-
173
- process.send?.({
174
- cmd,
175
- // @ts-expect-error TS2239
176
- requestId: message.requestId || undefined,
177
- data: isDebug ? e.stack : e.message,
178
- status: MessageStatus.ERROR,
179
- })
187
+ process.send?.({ cmd, requestId, data: errorPayload, status: MessageStatus.ERROR });
180
188
  }
181
189
  }