Skip to content

Commit

Permalink
fix(core): support onerror for js
Browse files Browse the repository at this point in the history
  • Loading branch information
etkmao committed Sep 5, 2023
1 parent d9efa37 commit 8ff4cad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 42 deletions.
20 changes: 7 additions & 13 deletions driver/js/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,28 +602,22 @@ void Scope::UnloadInstance(const std::shared_ptr<HippyValue>& value) {
void Scope::SetCallbackForUriLoader() {
auto the_loader = loader_.lock();
if (the_loader) {
the_loader->SetRequestTimePerformanceCallback([WEAK_THIS](const string_view& uri, const TimePoint& start, const TimePoint& end) {
the_loader->SetRequestResultCallback([WEAK_THIS](const string_view& uri,
const TimePoint& start, const TimePoint& end,
const int32_t ret_code, const string_view& error_msg) {
DEFINE_AND_CHECK_SELF(Scope)
auto runner = self->GetTaskRunner();
if (runner) {
auto task = [weak_this, uri, start, end]() {
auto task = [weak_this, uri, start, end, ret_code, error_msg]() {
DEFINE_AND_CHECK_SELF(Scope)
auto entry = self->GetPerformance()->PerformanceResource(uri);
if (entry) {
entry->SetLoadSourceStart(start);
entry->SetLoadSourceEnd(end);
}
};
runner->PostTask(std::move(task));
}
});
the_loader->SetRequestErrorCallback([WEAK_THIS](const string_view& uri, const int32_t ret_code, const string_view& error_msg) {
DEFINE_AND_CHECK_SELF(Scope)
auto runner = self->GetTaskRunner();
if (runner) {
auto task = [weak_this, uri, ret_code, error_msg]() {
DEFINE_AND_CHECK_SELF(Scope)
self->HandleUriLoaderError(uri, ret_code, error_msg);
if (ret_code != 0) {
self->HandleUriLoaderError(uri, ret_code, error_msg);
}
};
runner->PostTask(std::move(task));
}
Expand Down
7 changes: 2 additions & 5 deletions modules/vfs/ios/VFSUriLoader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,8 @@
VFSHandlerCompletionBlock callback = ^(NSData *data, NSURLResponse *response, NSError *error) {
auto endPoint = footstone::TimePoint::SystemNow();
string_view uri(NSStringToU16StringView([[response URL] absoluteString]));
DoRequestTimePerformanceCallback(uri, startPoint, endPoint);
if (error.code != 0) {
string_view msg([error.localizedDescription UTF8String]?:"");
DoRequestErrorCallback(uri, static_cast<int32_t>(error.code), msg);
}
string_view msg([error.localizedDescription UTF8String]?:"");
DoRequestResultCallback(uri, startPoint, endPoint, static_cast<int32_t>(error.code), msg);
if (completion) {
completion(data, response, error);
}
Expand Down
16 changes: 8 additions & 8 deletions modules/vfs/native/include/vfs/uri_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ class UriLoader: public std::enable_shared_from_this<UriLoader> {
using WorkerManager = footstone::WorkerManager;
using bytes = vfs::UriHandler::bytes;
using RetCode = vfs::JobResponse::RetCode;
using RequestTimePerformanceCallback = std::function<void(const string_view& uri, const TimePoint& start, const TimePoint& end)>;
using RequestErrorCallback = std::function<void(const string_view& uri, const int32_t ret_code, const string_view& error_msg)>;
using RequestResultCallback = std::function<void(const string_view& uri,
const TimePoint& start, const TimePoint& end,
const int32_t ret_code, const string_view& error_msg)>;

UriLoader();
virtual ~UriLoader() = default;
Expand Down Expand Up @@ -76,12 +77,12 @@ class UriLoader: public std::enable_shared_from_this<UriLoader> {

void Terminate();

void SetRequestTimePerformanceCallback(const RequestTimePerformanceCallback& cb) { on_request_time_performance_ = cb; }
void SetRequestErrorCallback(const RequestErrorCallback& cb) { on_request_error_ = cb; }
void SetRequestResultCallback(const RequestResultCallback& cb) { on_request_result_ = cb; }

protected:
void DoRequestTimePerformanceCallback(const string_view& uri, const TimePoint& start, const TimePoint& end);
void DoRequestErrorCallback(const string_view& uri, const int32_t ret_code, const string_view& error_msg);
void DoRequestResultCallback(const string_view& uri,
const TimePoint& start, const TimePoint& end,
const int32_t ret_code, const string_view& error_msg);

private:
std::shared_ptr<UriHandler> GetNextHandler(std::list<std::shared_ptr<UriHandler>>::iterator& cur,
Expand All @@ -97,8 +98,7 @@ class UriLoader: public std::enable_shared_from_this<UriLoader> {
std::list<std::shared_ptr<UriHandler>> interceptor_;
std::mutex mutex_;

RequestTimePerformanceCallback on_request_time_performance_;
RequestErrorCallback on_request_error_;
RequestResultCallback on_request_result_;
};

}
Expand Down
25 changes: 9 additions & 16 deletions modules/vfs/native/src/uri_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ void UriLoader::RequestUntrustedContent(const std::shared_ptr<RequestJob>& reque

// performance end time
auto end_time = TimePoint::SystemNow();
DoRequestTimePerformanceCallback(request->GetUri(), start_time, end_time);
if (response->GetRetCode() != JobResponse::RetCode::Success) {
DoRequestErrorCallback(request->GetUri(), static_cast<int32_t>(response->GetRetCode()), response->GetErrorMessage());
}
DoRequestResultCallback(request->GetUri(), start_time, end_time,
static_cast<int32_t>(response->GetRetCode()), response->GetErrorMessage());
}

void UriLoader::RequestUntrustedContent(const std::shared_ptr<RequestJob>& request,
Expand Down Expand Up @@ -157,10 +155,8 @@ void UriLoader::RequestUntrustedContent(const std::shared_ptr<RequestJob>& reque

// performance end time
auto end_time = TimePoint::SystemNow();
self->DoRequestTimePerformanceCallback(request->GetUri(), start_time, end_time);
if (response->GetRetCode() != JobResponse::RetCode::Success) {
self->DoRequestErrorCallback(request->GetUri(), static_cast<int32_t>(response->GetRetCode()), response->GetErrorMessage());
}
self->DoRequestResultCallback(request->GetUri(), start_time, end_time,
static_cast<int32_t>(response->GetRetCode()), response->GetErrorMessage());

orig_cb(response);
};
Expand Down Expand Up @@ -188,16 +184,13 @@ std::string UriLoader::GetScheme(const UriLoader::string_view& uri) {
return {};
}

void UriLoader::DoRequestTimePerformanceCallback(const string_view& uri, const TimePoint& start, const TimePoint& end) {
if (on_request_time_performance_ != nullptr) {
on_request_time_performance_(uri, start, end);
void UriLoader::DoRequestResultCallback(const string_view& uri,
const TimePoint& start, const TimePoint& end,
const int32_t ret_code, const string_view& error_msg) {
if (on_request_result_ != nullptr) {
on_request_result_(uri, start, end, ret_code, error_msg);
}
}

void UriLoader::DoRequestErrorCallback(const string_view& uri, const int32_t ret_code, const string_view& error_msg) {
if (on_request_error_ != nullptr) {
on_request_error_(uri, ret_code, error_msg);
}
}
}
}

0 comments on commit 8ff4cad

Please sign in to comment.