File tree Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Expand file tree Collapse file tree 2 files changed +26
-6
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,8 @@ export class VaultRequestError extends VaultError {
40
40
}
41
41
}
42
42
43
+ export class VaultDecryptionKeyNotFoundError extends VaultRequestError { }
44
+
43
45
export interface VaultRequestOptions {
44
46
retryWithTokenRenew ?: boolean ;
45
47
acceptedReturnCodes ?: number [ ] ;
@@ -172,12 +174,32 @@ export class Vault {
172
174
body : res . body ,
173
175
} ;
174
176
}
175
- throw new VaultRequestError (
177
+ const tmpErr = new VaultRequestError (
176
178
`Request to ${ requestOptions . uri . toString ( ) } failed (Status ${ errorResponse . statusCode } )` ,
177
179
errorResponse ,
178
180
) ;
181
+
182
+ throw this . convertToSpecificError ( tmpErr ) ;
179
183
}
180
184
181
185
return res . body ;
182
186
}
187
+
188
+ private convertToSpecificError ( error : VaultRequestError ) : VaultRequestError {
189
+ if ( this . checkError ( error , 400 , "encryption key not found" ) ) {
190
+ return new VaultDecryptionKeyNotFoundError ( `DecryptionKeyNotFound: ${ error . message } ` , error . response ) ;
191
+ }
192
+ return error ;
193
+ }
194
+
195
+ private checkError ( error : VaultRequestError , expectedCode : number , expectedMsg : string ) : boolean {
196
+ const { statusCode, body } = error . response ;
197
+ if ( expectedCode !== statusCode ) {
198
+ return false ;
199
+ }
200
+
201
+ const errors = body ?. errors ?? [ ] ;
202
+
203
+ return errors . some ( ( e ) => e . includes ( expectedMsg ) ) ;
204
+ }
183
205
}
Original file line number Diff line number Diff line change 1
- import { Vault } from "../../src" ;
1
+ import { Vault , VaultDecryptionKeyNotFoundError } from "../../src" ;
2
2
import { TransitVaultClient } from "../../src" ;
3
3
import * as util from "util" ;
4
4
@@ -133,8 +133,7 @@ describe("Transit Vault Client", () => {
133
133
try {
134
134
await client . decryptText ( "unknownkey" , encrypted ) ;
135
135
} catch ( err ) {
136
- testLogger ( err . response . body . errors ) ;
137
- expect ( err . response . statusCode ) . toEqual ( 400 ) ;
136
+ expect ( err ) . toBeInstanceOf ( VaultDecryptionKeyNotFoundError ) ;
138
137
}
139
138
} ) ;
140
139
@@ -153,8 +152,7 @@ describe("Transit Vault Client", () => {
153
152
] ,
154
153
} ) ;
155
154
} catch ( err ) {
156
- testLogger ( err . response . body . errors ) ;
157
- expect ( err . response . statusCode ) . toEqual ( 400 ) ;
155
+ expect ( err ) . toBeInstanceOf ( VaultDecryptionKeyNotFoundError ) ;
158
156
}
159
157
} ) ;
160
158
} ) ;
You can’t perform that action at this time.
0 commit comments