2019-08-19 09:10:18 -07: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-10 19:00:37 -06:00
|
|
|
#include <string_view>
|
|
|
|
|
2019-09-06 14:52:54 +09:00
|
|
|
#include "shell/common/gin_helper/error_thrower.h"
|
2019-08-19 09:10:18 -07:00
|
|
|
|
2019-09-06 14:52:54 +09:00
|
|
|
#include "gin/converter.h"
|
2019-08-19 09:10:18 -07:00
|
|
|
|
2019-09-06 14:52:54 +09:00
|
|
|
namespace gin_helper {
|
2019-08-19 09:10:18 -07:00
|
|
|
|
2025-04-01 17:25:29 -05:00
|
|
|
v8::Isolate* ErrorThrower::isolate() const {
|
|
|
|
// Callers should prefer to specify the isolate in the constructor,
|
|
|
|
// since GetCurrent() uses atomic loads and is thus a bit costly to invoke
|
|
|
|
return isolate_ ? isolate_.get() : v8::Isolate::GetCurrent();
|
|
|
|
}
|
2019-08-19 09:10:18 -07:00
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::ThrowError(const std::string_view err_msg) const {
|
2019-08-19 09:10:18 -07:00
|
|
|
Throw(v8::Exception::Error, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::ThrowTypeError(const std::string_view err_msg) const {
|
2019-08-19 09:10:18 -07:00
|
|
|
Throw(v8::Exception::TypeError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::ThrowRangeError(const std::string_view err_msg) const {
|
2019-08-19 09:10:18 -07:00
|
|
|
Throw(v8::Exception::RangeError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::ThrowReferenceError(const std::string_view err_msg) const {
|
2019-08-19 09:10:18 -07:00
|
|
|
Throw(v8::Exception::ReferenceError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::ThrowSyntaxError(const std::string_view err_msg) const {
|
2019-08-19 09:10:18 -07:00
|
|
|
Throw(v8::Exception::SyntaxError, err_msg);
|
|
|
|
}
|
|
|
|
|
2024-01-10 19:00:37 -06:00
|
|
|
void ErrorThrower::Throw(ErrorGenerator gen,
|
|
|
|
const std::string_view err_msg) const {
|
2025-04-01 17:25:29 -05:00
|
|
|
v8::Isolate* isolate = this->isolate();
|
|
|
|
|
|
|
|
if (!isolate->IsExecutionTerminating())
|
|
|
|
isolate->ThrowException(gen(gin::StringToV8(isolate, err_msg), {}));
|
2019-09-06 14:52:54 +09:00
|
|
|
}
|
2019-08-19 09:10:18 -07:00
|
|
|
|
2019-09-06 14:52:54 +09:00
|
|
|
} // namespace gin_helper
|