1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "tonic/logging/dart_error.h"
6#include "tonic/converter/dart_converter.h"
7
8#include "tonic/common/macros.h"
9
10namespace tonic {
11namespace DartError {
12const char kInvalidArgument[] = "Invalid argument.";
13} // namespace DartError
14
15bool LogIfError(Dart_Handle handle) {
16 if (Dart_IsUnhandledExceptionError(handle)) {
17 Dart_Handle stack_trace_handle = Dart_ErrorGetStackTrace(handle);
18 const std::string stack_trace =
19 tonic::StdStringFromDart(Dart_ToString(stack_trace_handle));
20 tonic::Log("Dart Unhandled Exception: %s", stack_trace.c_str());
21 return true;
22 } else if (Dart_IsError(handle)) {
23 tonic::Log("Dart Error: %s", Dart_GetError(handle));
24 return true;
25 } else {
26 return false;
27 }
28}
29
30DartErrorHandleType GetErrorHandleType(Dart_Handle handle) {
31 if (Dart_IsCompilationError(handle)) {
32 return kCompilationErrorType;
33 } else if (Dart_IsApiError(handle)) {
34 return kApiErrorType;
35 } else if (Dart_IsError(handle)) {
36 return kUnknownErrorType;
37 } else {
38 return kNoError;
39 }
40}
41
42int GetErrorExitCode(Dart_Handle handle) {
43 if (Dart_IsCompilationError(handle)) {
44 return 254; // dart::bin::kCompilationErrorExitCode
45 } else if (Dart_IsApiError(handle)) {
46 return 253; // dart::bin::kApiErrorExitCode
47 } else if (Dart_IsError(handle)) {
48 return 255; // dart::bin::kErrorExitCode
49 } else {
50 return 0;
51 }
52}
53
54} // namespace tonic
55