@@ -187,51 +187,86 @@ void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) {
187
187
args.GetReturnValue ().Set (enabled);
188
188
}
189
189
190
- void CryptoErrorVector ::Capture () {
191
- clear ();
192
- while (auto err = ERR_get_error ()) {
190
+ void CryptoErrorStore ::Capture () {
191
+ errors_. clear ();
192
+ while (const uint32_t err = ERR_get_error ()) {
193
193
char buf[256 ];
194
194
ERR_error_string_n (err, buf, sizeof (buf));
195
- push_back (buf);
195
+ errors_. emplace_back (buf);
196
196
}
197
- std::reverse (begin (), end ());
197
+ std::reverse (std:: begin (errors_ ), std:: end (errors_ ));
198
198
}
199
199
200
- MaybeLocal<Value> CryptoErrorVector::ToException (
200
+ bool CryptoErrorStore::Empty () const {
201
+ return errors_.empty ();
202
+ }
203
+
204
+ template <typename ... Args>
205
+ void CryptoErrorStore::Insert (const NodeCryptoError error, Args&&... args) {
206
+ const char * error_string;
207
+ switch (error) {
208
+ case NodeCryptoError::CIPHER_JOB_FAILED:
209
+ error_string = " Cipher job failed" ;
210
+ break ;
211
+ case NodeCryptoError::DERIVING_BITS_FAILED:
212
+ error_string = " Deriving bits failed" ;
213
+ break ;
214
+ case NodeCryptoError::ENGINE_NOT_FOUND:
215
+ error_string = " Engine \" %s\" was not found" ;
216
+ break ;
217
+ case NodeCryptoError::INVALID_KEY_TYPE:
218
+ error_string = " Invalid key type" ;
219
+ break ;
220
+ case NodeCryptoError::KEY_GENERATION_JOB_FAILED:
221
+ error_string = " Key generation failed" ;
222
+ break ;
223
+ case NodeCryptoError::NO_ERROR:
224
+ error_string = " No error" ;
225
+ break ;
226
+ }
227
+ errors.emplace_back (SPrintF (error_string,
228
+ std::forward<Args>(args)...));
229
+ }
230
+
231
+ MaybeLocal<Value> CryptoErrorStore::ToException (
201
232
Environment* env,
202
233
Local<String> exception_string) const {
203
234
if (exception_string.IsEmpty ()) {
204
- CryptoErrorVector copy (*this );
205
- if (copy.empty ()) copy.push_back (" no error" ); // But possibly a bug...
235
+ CryptoErrorStore copy (*this );
236
+ if (copy.Empty ()) {
237
+ // But possibly a bug...
238
+ copy.Insert (NodeCryptoError::NO_ERROR);
239
+ }
206
240
// Use last element as the error message, everything else goes
207
241
// into the .opensslErrorStack property on the exception object.
242
+ const std::string& last_error_string = copy.errors_ .back ();
208
243
Local<String> exception_string;
209
244
if (!String::NewFromUtf8 (
210
245
env->isolate (),
211
- copy. back (). data () ,
246
+ last_error_string ,
212
247
NewStringType::kNormal ,
213
- copy. back () .size ()).ToLocal (&exception_string)) {
248
+ last_error_string .size ()).ToLocal (&exception_string)) {
214
249
return MaybeLocal<Value>();
215
250
}
216
- copy.pop_back ();
251
+ copy.errors_ . pop_back ();
217
252
return copy.ToException (env, exception_string);
218
253
}
219
254
220
255
Local<Value> exception_v = Exception::Error (exception_string);
221
256
CHECK (!exception_v.IsEmpty ());
222
257
223
- if (!empty ()) {
224
- CHECK (exception_v->IsObject ());
225
- Local<Object> exception = exception_v.As <Object>();
226
- Local<Value> stack;
227
- if (!ToV8Value (env->context (), *this ).ToLocal (&stack) ||
228
- exception ->Set (env->context (), env->openssl_error_stack (), stack)
229
- .IsNothing ()) {
230
- return MaybeLocal<Value>();
231
- }
258
+ if (Empty ()) {
259
+ return exception_v;
232
260
}
233
261
234
- return exception_v;
262
+ CHECK (exception_v->IsObject ());
263
+ Local<Object> exception = exception_v.As <Object>();
264
+ Local<Value> stack;
265
+ if (!ToV8Value (env->context (), *this ).ToLocal (&stack) ||
266
+ exception ->Set (env->context (), env->openssl_error_stack (), stack)
267
+ .IsNothing ()) {
268
+ return MaybeLocal<Value>();
269
+ }
235
270
}
236
271
237
272
ByteSource::ByteSource (ByteSource&& other) noexcept
@@ -509,7 +544,7 @@ void ThrowCryptoError(Environment* env,
509
544
Local<Object> obj;
510
545
if (!String::NewFromUtf8 (env->isolate (), message).ToLocal (&exception_string))
511
546
return ;
512
- CryptoErrorVector errors;
547
+ CryptoErrorStore errors;
513
548
errors.Capture ();
514
549
if (!errors.ToException (env, exception_string).ToLocal (&exception ) ||
515
550
!exception ->ToObject (env->context ()).ToLocal (&obj) ||
@@ -520,7 +555,7 @@ void ThrowCryptoError(Environment* env,
520
555
}
521
556
522
557
#ifndef OPENSSL_NO_ENGINE
523
- EnginePointer LoadEngineById (const char * id, CryptoErrorVector * errors) {
558
+ EnginePointer LoadEngineById (const char * id, CryptoErrorStore * errors) {
524
559
MarkPopErrorOnReturn mark_pop_error_on_return;
525
560
526
561
EnginePointer engine (ENGINE_by_id (id));
@@ -539,14 +574,14 @@ EnginePointer LoadEngineById(const char* id, CryptoErrorVector* errors) {
539
574
if (ERR_get_error () != 0 ) {
540
575
errors->Capture ();
541
576
} else {
542
- errors->push_back ( std::string ( " Engine \" " ) + id + " \" was not found " );
577
+ errors->Insert (NodeCryptoError::ENGINE_NOT_FOUND, id );
543
578
}
544
579
}
545
580
546
581
return engine;
547
582
}
548
583
549
- bool SetEngine (const char * id, uint32_t flags, CryptoErrorVector * errors) {
584
+ bool SetEngine (const char * id, uint32_t flags, CryptoErrorStore * errors) {
550
585
ClearErrorOnReturn clear_error_on_return;
551
586
EnginePointer engine = LoadEngineById (id, errors);
552
587
if (!engine)
0 commit comments