Skip to content

Commit 1cd2431

Browse files
juan-fernandeziunanua
authored andcommitted
Fix log error message when intake request fails (#2757)
1 parent ea57ecc commit 1cd2431

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

packages/dd-trace/src/exporters/common/request.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,16 @@ function request (data, options, callback) {
9898
if (res.statusCode >= 200 && res.statusCode <= 299) {
9999
callback(null, responseData, res.statusCode)
100100
} else {
101-
const fullUrl = `${options.url || options.hostname || `localhost:${options.port}`}${options.path}`
102-
// eslint-disable-next-line
103-
let errorMessage = `Error from ${fullUrl}: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}.`
101+
let errorMessage = ''
102+
try {
103+
const fullUrl = new URL(
104+
options.path,
105+
options.url || options.hostname || `http://localhost:${options.port}`
106+
).href
107+
errorMessage = `Error from ${fullUrl}: ${res.statusCode} ${http.STATUS_CODES[res.statusCode]}.`
108+
} catch (e) {
109+
// ignore error
110+
}
104111
if (responseData) {
105112
errorMessage += ` Response from the endpoint: "${responseData}"`
106113
}

packages/dd-trace/test/ci-visibility/exporters/git/git_metadata.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('git_metadata', () => {
9191

9292
gitMetadata.sendGitMetadata(new URL('https://api.test.com'), false, (err) => {
9393
// eslint-disable-next-line
94-
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"')
94+
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"')
9595
// to check that it is not called
9696
expect(scope.isDone()).to.be.false
9797
expect(scope.pendingMocks()).to.contain('POST https://api.test.com:443/api/v2/git/repository/packfile')

packages/dd-trace/test/exporters/common/request.spec.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,33 @@ describe('request', function () {
7979
})
8080

8181
it('should handle an http error', done => {
82-
nock('http://localhost:80')
82+
nock('http://localhost:8080')
83+
.put('/path')
84+
.reply(400)
85+
86+
request(Buffer.from(''), {
87+
path: '/path',
88+
method: 'PUT',
89+
port: 8080
90+
}, err => {
91+
expect(err).to.be.instanceof(Error)
92+
expect(err.message).to.equal('Error from http://localhost:8080/path: 400 Bad Request.')
93+
done()
94+
})
95+
})
96+
97+
it('should handle an http error when url is specified', done => {
98+
nock('http://api.datadog.com')
8399
.put('/path')
84100
.reply(400)
85101

86102
request(Buffer.from(''), {
87103
path: '/path',
88104
method: 'PUT',
89-
port: 80
105+
url: new URL('http://api.datadog.com/')
90106
}, err => {
91107
expect(err).to.be.instanceof(Error)
92-
expect(err.message).to.equal('Error from localhost:80/path: 400 Bad Request.')
108+
expect(err.message).to.equal('Error from http://api.datadog.com/path: 400 Bad Request.')
93109
done()
94110
})
95111
})

0 commit comments

Comments
 (0)