11import {
2- ErrorCode ,
2+ ErrorResponseDataSchema ,
33 GetResourceInfoResponseData ,
44 GetResourceInfoResponseDataSchema ,
55 ImportRequestData ,
@@ -12,17 +12,19 @@ import {
1212 PlanRequestData ,
1313 PlanResponseData ,
1414 PlanResponseDataSchema ,
15+ PluginErrorData ,
1516 ResourceJson ,
1617 ValidateResponseData ,
1718 ValidateResponseDataSchema ,
1819} from '@codifycli/schemas' ;
1920
2021import { ResourcePlan } from '../entities/plan.js' ;
2122import { ResourceConfig } from '../entities/resource-config.js' ;
22- import { PluginApplyValidationError } from '../common/errors.js' ;
23+ import { PluginError } from '../common/errors.js' ;
2324import { ajv } from '../utils/ajv.js' ;
2425import { PluginProcess } from './plugin-process.js' ;
2526
27+ const errorResponseValidator = ajv . compile ( ErrorResponseDataSchema ) ;
2628const initializeResponseValidator = ajv . compile ( InitializeResponseDataSchema ) ;
2729const validateResponseValidator = ajv . compile ( ValidateResponseDataSchema ) ;
2830const getResourceInfoResponseValidator = ajv . compile ( GetResourceInfoResponseDataSchema ) ;
@@ -69,9 +71,9 @@ export class Plugin implements IPlugin {
6971 async validate ( configs : ResourceConfig [ ] ) : Promise < ValidateResponseData > {
7072 const jsonConfigs = configs . map ( ( c ) => c . toJson ( ) ) ;
7173 const result = await this . process ! . sendMessageForResult ( 'validate' , { configs : jsonConfigs } ) ;
72-
74+
7375 if ( ! result . isSuccessful ( ) ) {
74- throw new Error ( `Validate error for plugin: " ${ this . name } " \n\n ${ JSON . stringify ( result . data , null , 2 ) } ` ) ;
76+ throw new PluginError ( this . name , 'validate' , this . toErrorData ( result . data ) ) ;
7577 }
7678
7779 if ( ! this . validateValidateResponse ( result . data ) ) {
@@ -85,7 +87,7 @@ export class Plugin implements IPlugin {
8587 const result = await this . process ! . sendMessageForResult ( 'getResourceInfo' , { type } ) ;
8688
8789 if ( ! result . isSuccessful ( ) ) {
88- throw new Error ( `Unable to get info for resource: " ${ type } " from plugin: " ${ this . name } " \n\n` + result . data ) ;
90+ throw new PluginError ( this . name , type , this . toErrorData ( result . data ) ) ;
8991 }
9092
9193 if ( ! this . validateGetResourceInfoResponse ( result . data ) ) {
@@ -102,7 +104,7 @@ export class Plugin implements IPlugin {
102104 } ) ;
103105
104106 if ( ! result . isSuccessful ( ) ) {
105- throw new Error ( `Unable to match resource: " ${ resource . type } " from plugin: " ${ this . name } " \n\n` + result . data ) ;
107+ throw new PluginError ( this . name , resource . type , this . toErrorData ( result . data ) ) ;
106108 }
107109
108110 if ( ! this . validateMatchResponse ( result . data ) ) {
@@ -112,12 +114,11 @@ export class Plugin implements IPlugin {
112114 return result . data ;
113115 }
114116
115-
116117 async import ( config : ResourceJson , autoSearchAll = false ) : Promise < ImportResponseData > {
117118 const result = await this . process ! . sendMessageForResult ( 'import' , < ImportRequestData > { ...config , autoSearchAll } ) ;
118119
119120 if ( ! result . isSuccessful ( ) ) {
120- throw new Error ( `Unable import resource ${ config . core . type } with plugin: " ${ this . name } " \n\n` + result . data ) ;
121+ throw new PluginError ( this . name , config . core . type , this . toErrorData ( result . data ) ) ;
121122 }
122123
123124 if ( ! this . validateImportResponse ( result . data ) ) {
@@ -128,13 +129,10 @@ export class Plugin implements IPlugin {
128129 }
129130
130131 async plan ( request : PlanRequestData ) : Promise < ResourcePlan > {
131- const result = await this . process ! . sendMessageForResult (
132- 'plan' ,
133- request
134- ) ;
132+ const result = await this . process ! . sendMessageForResult ( 'plan' , request ) ;
135133
136134 if ( ! result . isSuccessful ( ) ) {
137- throw new Error ( `Plan error for plugin: " ${ this . name } ", resource: " ${ request . core . type } " \n\n` + result . data ) ;
135+ throw new PluginError ( this . name , request . core . type , this . toErrorData ( result . data ) ) ;
138136 }
139137
140138 if ( ! this . validatePlanResponse ( result . data ) ) {
@@ -148,25 +146,23 @@ export class Plugin implements IPlugin {
148146 const result = await this . process ! . sendMessageForResult ( 'apply' , { plan } ) ;
149147
150148 if ( ! result . isSuccessful ( ) ) {
151- const data = result . data as any ;
152-
153- if ( data ?. errorCode === ErrorCode . APPLY_VALIDATION && data . plan ) {
154- throw new PluginApplyValidationError ( new ResourcePlan ( data . plan ) ) ;
155- }
156-
157- const message = typeof data === 'string'
158- ? data
159- : ( data ?. message ?? JSON . stringify ( data , null , 2 ) ) ;
160- throw new Error ( `Apply error for plugin: "${ this . name } ", resource: "${ plan . resourceType } " \n\n` + message ) ;
149+ throw new PluginError ( this . name , plan . resourceType , this . toErrorData ( result . data ) ) ;
161150 }
162151 }
163152
164153 async setVerbosityLevel ( verbosityLevel : number ) : Promise < void > {
165154 const result = await this . process ! . sendMessageForResult ( 'setVerbosityLevel' , { verbosityLevel } ) ;
166155
167156 if ( ! result . isSuccessful ( ) ) {
168- throw new Error ( `Set verbosity error for plugin: "${ this . name } " \n\n` + result . data ) ;
157+ throw new PluginError ( this . name , 'setVerbosityLevel' , this . toErrorData ( result . data ) ) ;
158+ }
159+ }
160+
161+ private toErrorData ( data : unknown ) : PluginErrorData {
162+ if ( errorResponseValidator ( data ) ) {
163+ return data as unknown as PluginErrorData ;
169164 }
165+ return { errorType : 'unknown' , message : typeof data === 'string' ? data : JSON . stringify ( data , null , 2 ) } ;
170166 }
171167
172168 kill ( ) {
0 commit comments