microsoft / typed-rest-client

Node Rest and Http Clients with typings for use with TypeScript
Other
675 stars 118 forks source link

Unable to read error message from response header #331

Closed snydergd closed 2 years ago

snydergd commented 2 years ago
  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 commented 2 years ago

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

github-actions[bot] commented 2 years ago

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

snydergd commented 2 years ago

@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.