diff --git a/shell/browser/api/electron_api_content_tracing.cc b/shell/browser/api/electron_api_content_tracing.cc index 25f95fbebe7f3..90da88d1baf4b 100644 --- a/shell/browser/api/electron_api_content_tracing.cc +++ b/shell/browser/api/electron_api_content_tracing.cc @@ -66,15 +66,26 @@ base::Optional CreateTemporaryFileOnIO() { void StopTracing(gin_helper::Promise promise, base::Optional file_path) { + auto resolve_or_reject = base::AdaptCallbackForRepeating(base::BindOnce( + [](gin_helper::Promise promise, + const base::FilePath& path, base::Optional error) { + if (error) { + promise.RejectWithErrorMessage(error.value()); + } else { + promise.Resolve(path); + } + }, + std::move(promise), *file_path)); if (file_path) { auto endpoint = TracingController::CreateFileEndpoint( - *file_path, base::AdaptCallbackForRepeating(base::BindOnce( - &gin_helper::Promise::ResolvePromise, - std::move(promise), *file_path))); - TracingController::GetInstance()->StopTracing(endpoint); + *file_path, base::BindRepeating(resolve_or_reject, base::nullopt)); + if (!TracingController::GetInstance()->StopTracing(endpoint)) { + resolve_or_reject.Run(base::make_optional( + "Failed to stop tracing (was a trace in progress?)")); + } } else { - promise.RejectWithErrorMessage( - "Failed to create temporary file for trace data"); + resolve_or_reject.Run( + base::make_optional("Failed to create temporary file for trace data")); } } diff --git a/spec-main/api-content-tracing-spec.ts b/spec-main/api-content-tracing-spec.ts index cf7772622c93e..d1370eb3e4308 100644 --- a/spec-main/api-content-tracing-spec.ts +++ b/spec-main/api-content-tracing-spec.ts @@ -119,6 +119,10 @@ ifdescribe(!(process.platform !== 'win32' && ['arm', 'arm64'].includes(process.a const resultFilePath = await record(/* options */ {}, /* outputFilePath */ undefined); expect(resultFilePath).to.be.a('string').that.is.not.empty('result path'); }); + + it('rejects if no trace is happening', async () => { + await expect(contentTracing.stopRecording()).to.be.rejected(); + }); }); describe('captured events', () => {