Skip to content

Unable to read error message from response header #331

Closed
@snydergd

Description

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions