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

Unable to read error message from response header #331

Closed
snydergd opened this issue Jun 13, 2022 · 3 comments
Closed

Unable to read error message from response header #331

snydergd opened this issue Jun 13, 2022 · 3 comments

Comments

@snydergd
Copy link

  1. Please check our current Issues to see if someone already reported this https://github.com/Microsoft/typed-rest-client/issues - Searched but could not find an existing one. -- Done
  2. Take a look at our Instructions for logging issues https://github.com/Microsoft/typed-rest-client/blob/master/CONTRIBUTING.md#instructions-for-logging-issues -- Done

Environment

Node version: 10
Npm version: 6.14.2
OS and version: Ubuntu 20.04.4
typed-rest-client version: 1.8.9

Issue Description

The API I am calling returns a 400 (bad request) response and an error is thrown by the client. I log the error to determine what happened, but all I have available to me is the result (if available) and the status code. There is an error message in one of the HTTP headers of the response, but I do not have access to that. Without being able to log these other details, it is difficult to debug what the problem is with the request.

Expected behaviour

I would like to suggest that the full response, or at least the response headers, be provided in the error object when these error responses are received.

Actual behaviour

I'm unable to see the error message from a response if it is inside a response header.

Steps to reproduce

See where the error object is being created/thrown, and you can reproduce by running the below script with nodejs:

const http = require('http');
const restClient = require('typed-rest-client/RestClient');

const server = http.createServer((req, res) => {
  res.statusCode = 400;
  res.statusMessage = "Bad Request";
  res.setHeader("X-Error-Message", "The header you provided is not recognized");
  res.end();
});
server.listen(8000);

async function main() {
    const client = new restClient.RestClient('rest-client', 'http://localhost:8000/');
    try {
        await client.get('/');
    } catch (e) {
        console.error("Error")
        console.error(e);
        console.error("JSON of error");
        console.error(JSON.stringify(e));
        console.error("Error 'Result'");
        console.error(e.result);
    }
    server.close();
}

main().catch(console.error);

There seems to be no way to retrieve the value The header you provided is not recognized from the resulting error.

Logs

The output of above looks like the following

Error
{ Error: Failed request: (400)
    at RestClient.<anonymous> (C:\Users\george\git\toolslang_javascript_ServiceNowApprovalCheckAdoExtension\Tasks\ValidateFederatedPipeline\ValidateFederatedPipelineV0\node_modules\typed-rest-client\RestClient.js:202:31)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\george\git\toolslang_javascript_ServiceNowApprovalCheckAdoExtension\Tasks\ValidateFederatedPipeline\ValidateFederatedPipelineV0\node_modules\typed-rest-client\RestClient.js:6:58)
    at process._tickCallback (internal/process/next_tick.js:68:7) statusCode: 400 }
JSON of error
{"statusCode":400}
Error 'Result'
undefined
@vmapetr
Copy link

vmapetr commented Jun 14, 2022

@snydergd Thanks for reporting! We are working on more prioritized issues now but will get back to this one soon.

@github-actions
Copy link

This issue has had no activity in 90 days. Please comment if it is not actually stale

@github-actions github-actions bot added the stale label Sep 12, 2022
@snydergd
Copy link
Author

@vmapetr thanks for following up! I just realized I did not provide much context on this and that it might be useful. I discovered the issue while using azure-devops-node-api in a nodejs task extension for Azure DevOps. Since that runs behind the scenes on a server, this means I was left guessing the error message or trying to recreating the request from that library locally to see what the error was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants