Skip to content
Merged
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
13 changes: 10 additions & 3 deletions packages/dd-trace/src/exporters/common/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ function request (data, options, callback) {
if (res.statusCode >= 200 && res.statusCode <= 299) {
callback(null, responseData, res.statusCode)
} else {
const fullUrl = `${options.url || options.hostname || `localhost:${options.port}`}${options.path}`
// eslint-disable-next-line
let errorMessage = `Error from ${fullUrl}: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}.`
let errorMessage = ''
try {
const fullUrl = new URL(
options.path,
options.url || options.hostname || `http://localhost:${options.port}`
).href
errorMessage = `Error from ${fullUrl}: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}.`
} catch (e) {
// ignore error
}
if (responseData) {
errorMessage += ` Response from the endpoint: "${responseData}"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('git_metadata', () => {

gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
// eslint-disable-next-line
expect(err.message).to.contain('Error fetching commits to exclude: Error from https://api.test.com//api/v2/git/repository/search_commits: 404 Not Found. Response from the endpoint: "Not found SHA"')
expect(err.message).to.contain('Error fetching commits to exclude: Error from https://api.test.com/api/v2/git/repository/search_commits: 404 Not Found. Response from the endpoint: "Not found SHA"')
// to check that it is not called
expect(scope.isDone()).to.be.false
expect(scope.pendingMocks()).to.contain('POST https://api.test.com:443/api/v2/git/repository/packfile')
Expand Down
22 changes: 19 additions & 3 deletions packages/dd-trace/test/exporters/common/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,33 @@ describe('request', function () {
})

it('should handle an http error', done => {
nock('http://localhost:80')
nock('http://localhost:8080')
.put('/path')
.reply(400)

request(Buffer.from(''), {
path: '/path',
method: 'PUT',
port: 8080
}, err => {
expect(err).to.be.instanceof(Error)
expect(err.message).to.equal('Error from http://localhost:8080/path: 400 Bad Request.')
done()
})
})

it('should handle an http error when url is specified', done => {
nock('http://api.datadog.com')
.put('/path')
.reply(400)

request(Buffer.from(''), {
path: '/path',
method: 'PUT',
port: 80
url: new URL('http://api.datadog.com/')
}, err => {
expect(err).to.be.instanceof(Error)
expect(err.message).to.equal('Error from localhost:80/path: 400 Bad Request.')
expect(err.message).to.equal('Error from http://api.datadog.com/path: 400 Bad Request.')
done()
})
})
Expand Down