GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_errors.cc Lines: 159 265 60.0 %
Date: 2019-03-02 22:23:06 Branches: 147 279 52.7 %

Line Branch Exec Source
1
#include <cerrno>
2
#include <cstdarg>
3
4
#include "node_errors.h"
5
#include "node_internals.h"
6
#ifdef NODE_REPORT
7
#include "node_report.h"
8
#endif
9
10
namespace node {
11
12
using errors::TryCatchScope;
13
using v8::Context;
14
using v8::Exception;
15
using v8::Function;
16
using v8::FunctionCallbackInfo;
17
using v8::HandleScope;
18
using v8::Int32;
19
using v8::Isolate;
20
using v8::Just;
21
using v8::Local;
22
using v8::Maybe;
23
using v8::MaybeLocal;
24
using v8::Message;
25
using v8::NewStringType;
26
using v8::Number;
27
using v8::Object;
28
using v8::ScriptOrigin;
29
using v8::String;
30
using v8::Undefined;
31
using v8::Value;
32
33
420
bool IsExceptionDecorated(Environment* env, Local<Value> er) {
34

840
  if (!er.IsEmpty() && er->IsObject()) {
35
414
    Local<Object> err_obj = er.As<Object>();
36
    auto maybe_value =
37
828
        err_obj->GetPrivate(env->context(), env->decorated_private_symbol());
38
    Local<Value> decorated;
39

828
    return maybe_value.ToLocal(&decorated) && decorated->IsTrue();
40
  }
41
6
  return false;
42
}
43
44
namespace per_process {
45
4292
static Mutex tty_mutex;
46
}  // namespace per_process
47
48
422
void AppendExceptionLine(Environment* env,
49
                         Local<Value> er,
50
                         Local<Message> message,
51
                         enum ErrorHandlingMode mode) {
52
452
  if (message.IsEmpty()) return;
53
54
422
  HandleScope scope(env->isolate());
55
  Local<Object> err_obj;
56

844
  if (!er.IsEmpty() && er->IsObject()) {
57
416
    err_obj = er.As<Object>();
58
  }
59
60
  // Print (filename):(line number): (message).
61
422
  ScriptOrigin origin = message->GetScriptOrigin();
62
814
  node::Utf8Value filename(env->isolate(), message->GetScriptResourceName());
63
422
  const char* filename_string = *filename;
64
1266
  int linenum = message->GetLineNumber(env->context()).FromJust();
65
  // Print line of source code.
66
844
  MaybeLocal<String> source_line_maybe = message->GetSourceLine(env->context());
67
  node::Utf8Value sourceline(env->isolate(),
68
814
                             source_line_maybe.ToLocalChecked());
69
422
  const char* sourceline_string = *sourceline;
70
422
  if (strstr(sourceline_string, "node-do-not-add-exception-line") != nullptr)
71
18
    return;
72
73
  // Because of how node modules work, all scripts are wrapped with a
74
  // "function (module, exports, __filename, ...) {"
75
  // to provide script local variables.
76
  //
77
  // When reporting errors on the first line of a script, this wrapper
78
  // function is leaked to the user. There used to be a hack here to
79
  // truncate off the first 62 characters, but it caused numerous other
80
  // problems when vm.runIn*Context() methods were used for non-module
81
  // code.
82
  //
83
  // If we ever decide to re-instate such a hack, the following steps
84
  // must be taken:
85
  //
86
  // 1. Pass a flag around to say "this code was wrapped"
87
  // 2. Update the stack frame output so that it is also correct.
88
  //
89
  // It would probably be simpler to add a line rather than add some
90
  // number of characters to the first line, since V8 truncates the
91
  // sourceline to 78 characters, and we end up not providing very much
92
  // useful debugging info to the user if we remove 62 characters.
93
94
1616
  int script_start = (linenum - origin.ResourceLineOffset()->Value()) == 1
95
652
                         ? origin.ResourceColumnOffset()->Value()
96
652
                         : 0;
97
1212
  int start = message->GetStartColumn(env->context()).FromMaybe(0);
98
1212
  int end = message->GetEndColumn(env->context()).FromMaybe(0);
99
404
  if (start >= script_start) {
100
404
    CHECK_GE(end, start);
101
404
    start -= script_start;
102
404
    end -= script_start;
103
  }
104
105
  char arrow[1024];
106
404
  int max_off = sizeof(arrow) - 2;
107
108
  int off = snprintf(arrow,
109
                     sizeof(arrow),
110
                     "%s:%i\n%s\n",
111
                     filename_string,
112
                     linenum,
113
404
                     sourceline_string);
114
404
  CHECK_GE(off, 0);
115
404
  if (off > max_off) {
116
2
    off = max_off;
117
  }
118
119
  // Print wavy underline (GetUnderline is deprecated).
120
2735
  for (int i = 0; i < start; i++) {
121

2331
    if (sourceline_string[i] == '\0' || off >= max_off) {
122
      break;
123
    }
124
2331
    CHECK_LT(off, max_off);
125
2331
    arrow[off++] = (sourceline_string[i] == '\t') ? '\t' : ' ';
126
  }
127
1722
  for (int i = start; i < end; i++) {
128

1344
    if (sourceline_string[i] == '\0' || off >= max_off) {
129
      break;
130
    }
131
1318
    CHECK_LT(off, max_off);
132
1318
    arrow[off++] = '^';
133
  }
134
404
  CHECK_LE(off, max_off);
135
404
  arrow[off] = '\n';
136
404
  arrow[off + 1] = '\0';
137
138
  Local<String> arrow_str =
139
404
      String::NewFromUtf8(env->isolate(), arrow, NewStringType::kNormal)
140
808
          .ToLocalChecked();
141
142

808
  const bool can_set_arrow = !arrow_str.IsEmpty() && !err_obj.IsEmpty();
143
  // If allocating arrow_str failed, print it out. There's not much else to do.
144
  // If it's not an error, but something needs to be printed out because
145
  // it's a fatal exception, also print it out from here.
146
  // Otherwise, the arrow property will be attached to the object and handled
147
  // by the caller.
148


532
  if (!can_set_arrow || (mode == FATAL_ERROR && !err_obj->IsNativeError())) {
149
12
    if (env->printed_error()) return;
150
12
    Mutex::ScopedLock lock(per_process::tty_mutex);
151
12
    env->set_printed_error(true);
152
153
12
    uv_tty_reset_mode();
154
12
    PrintErrorString("\n%s", arrow);
155
12
    return;
156
  }
157
158

1568
  CHECK(err_obj
159
            ->SetPrivate(
160
                env->context(), env->arrow_message_private_symbol(), arrow_str)
161
392
            .FromMaybe(false));
162
}
163
164
[[noreturn]] void Abort() {
165
  DumpBacktrace(stderr);
166
  fflush(stderr);
167
  ABORT_NO_BACKTRACE();
168
}
169
170
[[noreturn]] void Assert(const AssertionInfo& info) {
171
  char name[1024];
172
  GetHumanReadableProcessName(&name);
173
174
  fprintf(stderr,
175
          "%s: %s:%s%s Assertion `%s' failed.\n",
176
          name,
177
          info.file_line,
178
          info.function,
179
          *info.function ? ":" : "",
180
          info.message);
181
  fflush(stderr);
182
183
  Abort();
184
}
185
186
152
void ReportException(Environment* env,
187
                     Local<Value> er,
188
                     Local<Message> message) {
189
152
  CHECK(!er.IsEmpty());
190
152
  HandleScope scope(env->isolate());
191
192
152
  if (message.IsEmpty()) message = Exception::CreateMessage(env->isolate(), er);
193
194
152
  AppendExceptionLine(env, er, message, FATAL_ERROR);
195
196
  Local<Value> trace_value;
197
  Local<Value> arrow;
198
152
  const bool decorated = IsExceptionDecorated(env, er);
199
200

606
  if (er->IsUndefined() || er->IsNull()) {
201
4
    trace_value = Undefined(env->isolate());
202
  } else {
203
450
    Local<Object> err_obj = er->ToObject(env->context()).ToLocalChecked();
204
205
750
    if (!err_obj->Get(env->context(), env->stack_string())
206
450
             .ToLocal(&trace_value)) {
207
2
      trace_value = Undefined(env->isolate());
208
    }
209
    arrow =
210
300
        err_obj->GetPrivate(env->context(), env->arrow_message_private_symbol())
211
300
            .ToLocalChecked();
212
  }
213
214
304
  node::Utf8Value trace(env->isolate(), trace_value);
215
216
  // range errors have a trace member set to undefined
217

456
  if (trace.length() > 0 && !trace_value->IsUndefined()) {
218


423
    if (arrow.IsEmpty() || !arrow->IsString() || decorated) {
219
52
      PrintErrorString("%s\n", *trace);
220
    } else {
221
89
      node::Utf8Value arrow_string(env->isolate(), arrow);
222
89
      PrintErrorString("%s\n%s\n", *arrow_string, *trace);
223
    }
224
  } else {
225
    // this really only happens for RangeErrors, since they're the only
226
    // kind that won't have all this info in the trace, or when non-Error
227
    // objects are thrown manually.
228
    MaybeLocal<Value> message;
229
    MaybeLocal<Value> name;
230
231
11
    if (er->IsObject()) {
232
5
      Local<Object> err_obj = er.As<Object>();
233
15
      message = err_obj->Get(env->context(), env->message_string());
234
15
      name = err_obj->Get(env->context(), env->name_string());
235
    }
236
237


49
    if (message.IsEmpty() || message.ToLocalChecked()->IsUndefined() ||
238

37
        name.IsEmpty() || name.ToLocalChecked()->IsUndefined()) {
239
      // Not an error object. Just print as-is.
240
10
      String::Utf8Value message(env->isolate(), er);
241
242
      PrintErrorString("%s\n",
243
10
                       *message ? *message : "<toString() threw exception>");
244
    } else {
245
1
      node::Utf8Value name_string(env->isolate(), name.ToLocalChecked());
246
2
      node::Utf8Value message_string(env->isolate(), message.ToLocalChecked());
247
248


3
      if (arrow.IsEmpty() || !arrow->IsString() || decorated) {
249
1
        PrintErrorString("%s: %s\n", *name_string, *message_string);
250
      } else {
251
        node::Utf8Value arrow_string(env->isolate(), arrow);
252
        PrintErrorString(
253
            "%s\n%s: %s\n", *arrow_string, *name_string, *message_string);
254
1
      }
255
    }
256
  }
257
258
152
  fflush(stderr);
259
260
#if HAVE_INSPECTOR
261
304
  env->inspector_agent()->FatalException(er, message);
262
#endif
263
152
}
264
265
3
void ReportException(Environment* env, const v8::TryCatch& try_catch) {
266
3
  ReportException(env, try_catch.Exception(), try_catch.Message());
267
3
}
268
269
176
void PrintErrorString(const char* format, ...) {
270
  va_list ap;
271
176
  va_start(ap, format);
272
#ifdef _WIN32
273
  HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
274
275
  // Check if stderr is something other than a tty/console
276
  if (stderr_handle == INVALID_HANDLE_VALUE || stderr_handle == nullptr ||
277
      uv_guess_handle(_fileno(stderr)) != UV_TTY) {
278
    vfprintf(stderr, format, ap);
279
    va_end(ap);
280
    return;
281
  }
282
283
  // Fill in any placeholders
284
  int n = _vscprintf(format, ap);
285
  std::vector<char> out(n + 1);
286
  vsprintf(out.data(), format, ap);
287
288
  // Get required wide buffer size
289
  n = MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, nullptr, 0);
290
291
  std::vector<wchar_t> wbuf(n);
292
  MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, wbuf.data(), n);
293
294
  // Don't include the null character in the output
295
  CHECK_GT(n, 0);
296
  WriteConsoleW(stderr_handle, wbuf.data(), n - 1, nullptr, nullptr);
297
#else
298
176
  vfprintf(stderr, format, ap);
299
#endif
300
176
  va_end(ap);
301
176
}
302
303
[[noreturn]] void FatalError(const char* location, const char* message) {
304
  OnFatalError(location, message);
305
  // to suppress compiler warning
306
  ABORT();
307
}
308
309
void OnFatalError(const char* location, const char* message) {
310
  if (location) {
311
    PrintErrorString("FATAL ERROR: %s %s\n", location, message);
312
  } else {
313
    PrintErrorString("FATAL ERROR: %s\n", message);
314
  }
315
#ifdef NODE_REPORT
316
  Isolate* isolate = Isolate::GetCurrent();
317
  HandleScope handle_scope(isolate);
318
  Environment* env = Environment::GetCurrent(isolate);
319
  if (env == nullptr || env->isolate_data()->options()->report_on_fatalerror) {
320
    report::TriggerNodeReport(
321
        isolate, env, message, __func__, "", Local<String>());
322
  }
323
#endif  // NODE_REPORT
324
  fflush(stderr);
325
  ABORT();
326
}
327
328
namespace errors {
329
330
328962
TryCatchScope::~TryCatchScope() {
331


164481
  if (HasCaught() && !HasTerminated() && mode_ == CatchMode::kFatal) {
332
    HandleScope scope(env_->isolate());
333
    ReportException(env_, Exception(), Message());
334
    env_->Exit(7);
335
  }
336
164481
}
337
338
4
const char* errno_string(int errorno) {
339
#define ERRNO_CASE(e)                                                          \
340
  case e:                                                                      \
341
    return #e;
342



















4
  switch (errorno) {
343
#ifdef EACCES
344
    ERRNO_CASE(EACCES);
345
#endif
346
347
#ifdef EADDRINUSE
348
    ERRNO_CASE(EADDRINUSE);
349
#endif
350
351
#ifdef EADDRNOTAVAIL
352
    ERRNO_CASE(EADDRNOTAVAIL);
353
#endif
354
355
#ifdef EAFNOSUPPORT
356
    ERRNO_CASE(EAFNOSUPPORT);
357
#endif
358
359
#ifdef EAGAIN
360
    ERRNO_CASE(EAGAIN);
361
#endif
362
363
#ifdef EWOULDBLOCK
364
#if EAGAIN != EWOULDBLOCK
365
    ERRNO_CASE(EWOULDBLOCK);
366
#endif
367
#endif
368
369
#ifdef EALREADY
370
    ERRNO_CASE(EALREADY);
371
#endif
372
373
#ifdef EBADF
374
    ERRNO_CASE(EBADF);
375
#endif
376
377
#ifdef EBADMSG
378
    ERRNO_CASE(EBADMSG);
379
#endif
380
381
#ifdef EBUSY
382
    ERRNO_CASE(EBUSY);
383
#endif
384
385
#ifdef ECANCELED
386
    ERRNO_CASE(ECANCELED);
387
#endif
388
389
#ifdef ECHILD
390
1
    ERRNO_CASE(ECHILD);
391
#endif
392
393
#ifdef ECONNABORTED
394
    ERRNO_CASE(ECONNABORTED);
395
#endif
396
397
#ifdef ECONNREFUSED
398
    ERRNO_CASE(ECONNREFUSED);
399
#endif
400
401
#ifdef ECONNRESET
402
    ERRNO_CASE(ECONNRESET);
403
#endif
404
405
#ifdef EDEADLK
406
    ERRNO_CASE(EDEADLK);
407
#endif
408
409
#ifdef EDESTADDRREQ
410
    ERRNO_CASE(EDESTADDRREQ);
411
#endif
412
413
#ifdef EDOM
414
    ERRNO_CASE(EDOM);
415
#endif
416
417
#ifdef EDQUOT
418
    ERRNO_CASE(EDQUOT);
419
#endif
420
421
#ifdef EEXIST
422
    ERRNO_CASE(EEXIST);
423
#endif
424
425
#ifdef EFAULT
426
    ERRNO_CASE(EFAULT);
427
#endif
428
429
#ifdef EFBIG
430
    ERRNO_CASE(EFBIG);
431
#endif
432
433
#ifdef EHOSTUNREACH
434
    ERRNO_CASE(EHOSTUNREACH);
435
#endif
436
437
#ifdef EIDRM
438
    ERRNO_CASE(EIDRM);
439
#endif
440
441
#ifdef EILSEQ
442
    ERRNO_CASE(EILSEQ);
443
#endif
444
445
#ifdef EINPROGRESS
446
    ERRNO_CASE(EINPROGRESS);
447
#endif
448
449
#ifdef EINTR
450
    ERRNO_CASE(EINTR);
451
#endif
452
453
#ifdef EINVAL
454
    ERRNO_CASE(EINVAL);
455
#endif
456
457
#ifdef EIO
458
    ERRNO_CASE(EIO);
459
#endif
460
461
#ifdef EISCONN
462
    ERRNO_CASE(EISCONN);
463
#endif
464
465
#ifdef EISDIR
466
    ERRNO_CASE(EISDIR);
467
#endif
468
469
#ifdef ELOOP
470
    ERRNO_CASE(ELOOP);
471
#endif
472
473
#ifdef EMFILE
474
    ERRNO_CASE(EMFILE);
475
#endif
476
477
#ifdef EMLINK
478
    ERRNO_CASE(EMLINK);
479
#endif
480
481
#ifdef EMSGSIZE
482
    ERRNO_CASE(EMSGSIZE);
483
#endif
484
485
#ifdef EMULTIHOP
486
    ERRNO_CASE(EMULTIHOP);
487
#endif
488
489
#ifdef ENAMETOOLONG
490
    ERRNO_CASE(ENAMETOOLONG);
491
#endif
492
493
#ifdef ENETDOWN
494
    ERRNO_CASE(ENETDOWN);
495
#endif
496
497
#ifdef ENETRESET
498
    ERRNO_CASE(ENETRESET);
499
#endif
500
501
#ifdef ENETUNREACH
502
    ERRNO_CASE(ENETUNREACH);
503
#endif
504
505
#ifdef ENFILE
506
    ERRNO_CASE(ENFILE);
507
#endif
508
509
#ifdef ENOBUFS
510
    ERRNO_CASE(ENOBUFS);
511
#endif
512
513
#ifdef ENODATA
514
    ERRNO_CASE(ENODATA);
515
#endif
516
517
#ifdef ENODEV
518
    ERRNO_CASE(ENODEV);
519
#endif
520
521
#ifdef ENOENT
522
    ERRNO_CASE(ENOENT);
523
#endif
524
525
#ifdef ENOEXEC
526
    ERRNO_CASE(ENOEXEC);
527
#endif
528
529
#ifdef ENOLINK
530
    ERRNO_CASE(ENOLINK);
531
#endif
532
533
#ifdef ENOLCK
534
#if ENOLINK != ENOLCK
535
    ERRNO_CASE(ENOLCK);
536
#endif
537
#endif
538
539
#ifdef ENOMEM
540
    ERRNO_CASE(ENOMEM);
541
#endif
542
543
#ifdef ENOMSG
544
    ERRNO_CASE(ENOMSG);
545
#endif
546
547
#ifdef ENOPROTOOPT
548
    ERRNO_CASE(ENOPROTOOPT);
549
#endif
550
551
#ifdef ENOSPC
552
    ERRNO_CASE(ENOSPC);
553
#endif
554
555
#ifdef ENOSR
556
    ERRNO_CASE(ENOSR);
557
#endif
558
559
#ifdef ENOSTR
560
    ERRNO_CASE(ENOSTR);
561
#endif
562
563
#ifdef ENOSYS
564
    ERRNO_CASE(ENOSYS);
565
#endif
566
567
#ifdef ENOTCONN
568
    ERRNO_CASE(ENOTCONN);
569
#endif
570
571
#ifdef ENOTDIR
572
    ERRNO_CASE(ENOTDIR);
573
#endif
574
575
#ifdef ENOTEMPTY
576
#if ENOTEMPTY != EEXIST
577
    ERRNO_CASE(ENOTEMPTY);
578
#endif
579
#endif
580
581
#ifdef ENOTSOCK
582
    ERRNO_CASE(ENOTSOCK);
583
#endif
584
585
#ifdef ENOTSUP
586
    ERRNO_CASE(ENOTSUP);
587
#else
588
#ifdef EOPNOTSUPP
589
    ERRNO_CASE(EOPNOTSUPP);
590
#endif
591
#endif
592
593
#ifdef ENOTTY
594
    ERRNO_CASE(ENOTTY);
595
#endif
596
597
#ifdef ENXIO
598
    ERRNO_CASE(ENXIO);
599
#endif
600
601
#ifdef EOVERFLOW
602
    ERRNO_CASE(EOVERFLOW);
603
#endif
604
605
#ifdef EPERM
606
2
    ERRNO_CASE(EPERM);
607
#endif
608
609
#ifdef EPIPE
610
    ERRNO_CASE(EPIPE);
611
#endif
612
613
#ifdef EPROTO
614
    ERRNO_CASE(EPROTO);
615
#endif
616
617
#ifdef EPROTONOSUPPORT
618
    ERRNO_CASE(EPROTONOSUPPORT);
619
#endif
620
621
#ifdef EPROTOTYPE
622
    ERRNO_CASE(EPROTOTYPE);
623
#endif
624
625
#ifdef ERANGE
626
    ERRNO_CASE(ERANGE);
627
#endif
628
629
#ifdef EROFS
630
    ERRNO_CASE(EROFS);
631
#endif
632
633
#ifdef ESPIPE
634
    ERRNO_CASE(ESPIPE);
635
#endif
636
637
#ifdef ESRCH
638
1
    ERRNO_CASE(ESRCH);
639
#endif
640
641
#ifdef ESTALE
642
    ERRNO_CASE(ESTALE);
643
#endif
644
645
#ifdef ETIME
646
    ERRNO_CASE(ETIME);
647
#endif
648
649
#ifdef ETIMEDOUT
650
    ERRNO_CASE(ETIMEDOUT);
651
#endif
652
653
#ifdef ETXTBSY
654
    ERRNO_CASE(ETXTBSY);
655
#endif
656
657
#ifdef EXDEV
658
    ERRNO_CASE(EXDEV);
659
#endif
660
661
    default:
662
      return "";
663
  }
664
}
665
666
}  // namespace errors
667
668
3375
void DecorateErrorStack(Environment* env,
669
                        const errors::TryCatchScope& try_catch) {
670
3375
  Local<Value> exception = try_catch.Exception();
671
672
6487
  if (!exception->IsObject()) return;
673
674
268
  Local<Object> err_obj = exception.As<Object>();
675
676
268
  if (IsExceptionDecorated(env, err_obj)) return;
677
678
265
  AppendExceptionLine(env, exception, try_catch.Message(), CONTEXTIFY_ERROR);
679
265
  TryCatchScope try_catch_scope(env);  // Ignore exceptions below.
680
795
  MaybeLocal<Value> stack = err_obj->Get(env->context(), env->stack_string());
681
  MaybeLocal<Value> maybe_value =
682
530
      err_obj->GetPrivate(env->context(), env->arrow_message_private_symbol());
683
684
  Local<Value> arrow;
685

795
  if (!(maybe_value.ToLocal(&arrow) && arrow->IsString())) {
686
    return;
687
  }
688
689


1325
  if (stack.IsEmpty() || !stack.ToLocalChecked()->IsString()) {
690
2
    return;
691
  }
692
693
  Local<String> decorated_stack = String::Concat(
694
      env->isolate(),
695
      String::Concat(env->isolate(),
696
                     arrow.As<String>(),
697
                     FIXED_ONE_BYTE_STRING(env->isolate(), "\n")),
698
789
      stack.ToLocalChecked().As<String>());
699
789
  USE(err_obj->Set(env->context(), env->stack_string(), decorated_stack));
700
  err_obj->SetPrivate(
701
789
      env->context(), env->decorated_private_symbol(), True(env->isolate()));
702
}
703
704
1337
void FatalException(Isolate* isolate,
705
                    Local<Value> error,
706
                    Local<Message> message) {
707
1337
  CHECK(!error.IsEmpty());
708
1337
  HandleScope scope(isolate);
709
710
1337
  Environment* env = Environment::GetCurrent(isolate);
711
1337
  CHECK_NOT_NULL(env);  // TODO(addaleax): Handle nullptr here.
712
1337
  Local<Object> process_object = env->process_object();
713
1337
  Local<String> fatal_exception_string = env->fatal_exception_string();
714
  Local<Value> fatal_exception_function =
715
      process_object->Get(env->context(),
716
4011
                          fatal_exception_string).ToLocalChecked();
717
718
1337
  if (!fatal_exception_function->IsFunction()) {
719
    // Failed before the process._fatalException function was added!
720
    // this is probably pretty bad.  Nothing to do but report and exit.
721
    ReportException(env, error, message);
722
    env->Exit(6);
723
  } else {
724
1337
    errors::TryCatchScope fatal_try_catch(env);
725
726
    // Do not call FatalException when _fatalException handler throws
727
1337
    fatal_try_catch.SetVerbose(false);
728
729
    // This will return true if the JS layer handled it, false otherwise
730
    MaybeLocal<Value> caught = fatal_exception_function.As<Function>()->Call(
731
4011
        env->context(), process_object, 1, &error);
732
733
2518
    if (fatal_try_catch.HasTerminated()) return;
734
735
1335
    if (fatal_try_catch.HasCaught()) {
736
      // The fatal exception function threw, so we must exit
737
3
      ReportException(env, fatal_try_catch);
738
3
      env->Exit(7);
739
740
2664
    } else if (caught.ToLocalChecked()->IsFalse()) {
741
149
      ReportException(env, error, message);
742
743
      // fatal_exception_function call before may have set a new exit code ->
744
      // read it again, otherwise use default for uncaughtException 1
745
149
      Local<String> exit_code = env->exit_code_string();
746
      Local<Value> code;
747


745
      if (!process_object->Get(env->context(), exit_code).ToLocal(&code) ||
748
149
          !code->IsInt32()) {
749
1
        env->Exit(1);
750
      }
751
296
      env->Exit(code.As<Int32>()->Value());
752
1183
    }
753
1183
  }
754
}
755
756
15
void FatalException(const FunctionCallbackInfo<Value>& args) {
757
15
  Isolate* isolate = args.GetIsolate();
758
15
  Environment* env = Environment::GetCurrent(isolate);
759

15
  if (env != nullptr && env->abort_on_uncaught_exception()) {
760
    Abort();
761
  }
762
15
  Local<Value> exception = args[0];
763
15
  Local<Message> message = Exception::CreateMessage(isolate, exception);
764
15
  FatalException(isolate, exception, message);
765
3
}
766
767

12876
}  // namespace node