Skip to content
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

Return error object instead of message #82

Closed
wants to merge 3 commits into from

Conversation

vprintempsPei
Copy link
Contributor

Currently the error on endpoint only returns a message, and then the following console.log(error.httpStatus) returns undefined:

// someWrongUrl can be, for example: https://data.geopf.fr/wts?SERVICE=WMTS&VERSION=1.0.0
this.endpoint = new WmtsEndpoint(someWrongUrl);
try {
  await this.endpoint.isReady();
} catch (e) {
  console.log(e.httpStatus); 
  // returns undefined
  this.error = e.message;
  // returns a message
}

This behavior is due to the following line in eventHandler (utils.ts):

try {
  response = await handler(request.params);
} catch (e) {
  error = e;
}

It can be fixed as follows:

const eventHandler = async (request: WorkerRequest) => {
  if (request.taskName === taskName) {
    let response;

    // !! Update here !!
    let error: EndpointError;
    try {
      response = await handler(request.params);
    } catch (e) {

      // !! Update here !!
      error = {
        httpStatus: e.httpStatus,
        message: e.message,
        name: e.name
      };

    }
    const message = /** @type {WorkerResponse} */ {
      taskName,
      requestId: request.requestId,
      ...(response && { response }),
      ...(error && { error }),
    };
    if (useWorker) {
      scope.postMessage(message);
    } else {
      scope.dispatchEvent(
        new CustomEvent('ogc-client.response', {
          detail: message,
        })
      );
    }
  }
};

This pull request aims to return an error object that contains the error message in error.message and all other error information like httpStatus. Tests have been updated accordingly.

Let me know if i need to change/fix anything in the PR as i don't understand very well the eventHandler function

@jahow
Copy link
Member

jahow commented Nov 9, 2024

Hi @vprintempsPei , thanks for this PR. I looked into it and I feel like this shouldn't be needed. There can be different kinds of exception when loading an Endpoint (either EndpointError or ServiceExceptionError) and any of them can be returned. If there's no http code, then maybe it's because the error is not an HTTP error?

@jahow
Copy link
Member

jahow commented Nov 9, 2024

Thank you for raising this issue, indeed there was a problem where the specialized error types were reset in certain scenario! I created a bigger PR to address this and also improve the test coverage of that part.

@jahow jahow closed this Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants