Skip to content

Commit 7ae7d35

Browse files
author
Ammar Dodin
committed
🆕 add global transaction id to error body
1 parent 09cbf02 commit 7ae7d35

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

lib/requestwrapper.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { PassThrough as readableStream } from 'stream';
2626

2727
const pkg = require('../package.json');
2828
const isBrowser = typeof window === 'object';
29+
const globalTransactionId = 'x-global-transaction-id';
2930

3031
/**
3132
* @private
@@ -61,6 +62,9 @@ export function formatErrorIfExists(cb: Function): request.RequestCallback {
6162
error = new Error(error.message || error.error || error);
6263
error.body = body;
6364
}
65+
if (response && response.headers) {
66+
error[globalTransactionId] = response.headers[globalTransactionId];
67+
}
6468
cb(error, body, response);
6569
return;
6670
}
@@ -74,13 +78,14 @@ export function formatErrorIfExists(cb: Function): request.RequestCallback {
7478

7579
// for api-key services
7680
if (response.statusMessage === 'invalid-api-key') {
77-
cb(
78-
{
79-
error: response.statusMessage,
80-
code: response.statusMessage === 'invalid-api-key' ? 401 : 400
81-
},
82-
null
83-
);
81+
const error = {
82+
error: response.statusMessage,
83+
code: response.statusMessage === 'invalid-api-key' ? 401 : 400
84+
};
85+
if (response.headers) {
86+
error[globalTransactionId] = response.headers[globalTransactionId];
87+
}
88+
cb(error, null);
8489
return;
8590
}
8691

@@ -125,6 +130,9 @@ export function formatErrorIfExists(cb: Function): request.RequestCallback {
125130
}
126131
body = null;
127132
}
133+
if (error && response && response.headers) {
134+
error[globalTransactionId] = response.headers[globalTransactionId];
135+
}
128136
cb(error, body, response);
129137
return;
130138
};

0 commit comments

Comments
 (0)