2019-08-19 16:10:18 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-08-19 16:10:18 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "gin/converter.h"
|
2019-08-19 16:10:18 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
namespace gin_helper {
|
2019-08-19 16:10:18 +00:00
|
|
|
|
|
|
|
ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {}
|
|
|
|
|
|
|
|
// This constructor should be rarely if ever used, since
|
|
|
|
// v8::Isolate::GetCurrent() uses atomic loads and is thus a bit
|
|
|
|
// costly to invoke
|
|
|
|
ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::ThrowError(const std::string_view err_msg) const {
|
2019-08-19 16:10:18 +00:00
|
|
|
Throw(v8::Exception::Error, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::ThrowTypeError(const std::string_view err_msg) const {
|
2019-08-19 16:10:18 +00:00
|
|
|
Throw(v8::Exception::TypeError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::ThrowRangeError(const std::string_view err_msg) const {
|
2019-08-19 16:10:18 +00:00
|
|
|
Throw(v8::Exception::RangeError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::ThrowReferenceError(const std::string_view err_msg) const {
|
2019-08-19 16:10:18 +00:00
|
|
|
Throw(v8::Exception::ReferenceError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::ThrowSyntaxError(const std::string_view err_msg) const {
|
2019-08-19 16:10:18 +00:00
|
|
|
Throw(v8::Exception::SyntaxError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
void ErrorThrower::Throw(ErrorGenerator gen,
|
|
|
|
const std::string_view err_msg) const {
|
2023-10-02 22:01:07 +00:00
|
|
|
v8::Local<v8::Value> exception = gen(gin::StringToV8(isolate_, err_msg), {});
|
2019-09-06 05:52:54 +00:00
|
|
|
if (!isolate_->IsExecutionTerminating())
|
|
|
|
isolate_->ThrowException(exception);
|
|
|
|
}
|
2019-08-19 16:10:18 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
} // namespace gin_helper
|