Skip to content

feat: support onError api to catch errors in pre req #4581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bruno-app/src/components/CodeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ if (!SERVER_RENDERED) {
'req.setMaxRedirects(maxRedirects)',
'req.getTimeout()',
'req.setTimeout(timeout)',
'req.onError(function(err) {})',
'req.getExecutionMode()',
'bru',
'bru.cwd()',
Expand Down
18 changes: 18 additions & 0 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,15 @@ const registerNetworkIpc = (mainWindow) => {
};
}

// Call the error handler if it exists
if (typeof request.errorHandler === 'function') {
try {
request.errorHandler(error);
} catch (handlerError) {
console.error('Error in request error handler:', handlerError);
}
}

if (error?.response) {
response = error.response;

Expand Down Expand Up @@ -1098,6 +1107,15 @@ const registerNetworkIpc = (mainWindow) => {
...eventData
});
} catch (error) {
// Call the error handler if it exists
if (typeof request.errorHandler === 'function') {
try {
request.errorHandler(error);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Duplicate code can be extracted and reused
  2. Handle both sync and async callbacks
  3. Make it consistent whether to invoke or not on user cancellation of the request
  4. Should we invoke this errorHandler on soft errors?
  5. Any possibility of moving this to axios interceptors?
/**
 * Executes the custom error handler if it exists on the request
 * @param {Object} request - The request object that may contain an errorHandler
 * @param {Error} error - The error that occurred
 */
const executeRequestErrorHandler = async (request, error) => {
  if (!request || typeof request.errorHandler !== 'function') {
    return;
  }

  try {
    await request.errorHandler(error);
  } catch (handlerError) {
    console.error('Error in request error handler:', handlerError);
  }
};

} catch (handlerError) {
console.error('Error in request error handler:', handlerError);
}
}

if (error?.response && !axios.isCancel(error)) {
const { data, dataBuffer } = parseDataFromResponse(error.response);
error.response.data = data;
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-js/src/bruno-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class BrunoRequest {
this.req.timeout = timeout;
}

onError(callback) {
if (typeof callback === 'function') {
this.req.errorHandler = callback;
}
}

__safeParseJSON(str) {
try {
return JSON.parse(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ const addBrunoRequestShimToContext = (vm, req) => {
vm.setProp(reqObject, 'setTimeout', setTimeout);
setTimeout.dispose();

let onError = vm.newFunction('onError', function (callback) {
req.onError(vm.dump(callback));
});
vm.setProp(reqObject, 'onError', onError);
onError.dispose();

let disableParsingResponseJson = vm.newFunction('disableParsingResponseJson', function () {
req.disableParsingResponseJson();
});
Expand Down