Skip to content

Commit b9cc808

Browse files
ofrobotsDominicKramer
authored andcommitted
fix: properly handle hapi v16+ req.url (#311)
request.url can be a url.URL object. Fixes: googleapis/nodejs-error-reporting#196
1 parent 576dc95 commit b9cc808

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

  • handwritten/error-reporting

handwritten/error-reporting/src/request-extractors/hapi.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,15 @@ export function hapiRequestInformationExtractor(req?: hapi.Request) {
8181
return returnObject;
8282
}
8383

84-
returnObject
85-
.setMethod(req!.method)
86-
// TODO: Address the type conflict that requires a cast to string
87-
.setUrl(req!.url as {} as string)
84+
let urlString: string;
85+
if (is.string(req!.url)) {
86+
urlString = req!.url as {} as string;
87+
} else {
88+
urlString = req!.url.pathname;
89+
}
90+
91+
returnObject.setMethod(req!.method)
92+
.setUrl(urlString)
8893
.setUserAgent(req!.headers['user-agent'])
8994
.setReferrer(req!.headers.referrer)
9095
.setStatusCode(attemptToExtractStatusCode(req!))

handwritten/error-reporting/test/unit/request-extractors/hapi.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as hapi from 'hapi';
18+
import {URL} from 'url';
1819

1920
import {hapiRequestInformationExtractor} from '../../../src/request-extractors/hapi';
2021
import {Fuzzer} from '../../../utils/fuzzer';
@@ -120,5 +121,16 @@ describe('hapiRequestInformationExtractor behaviour', () => {
120121
ANOTHER_PARTIAL_REQ_DERIVATION_VALUE as {} as hapi.Request),
121122
ANOTHER_PARTIAL_REQ_EXPECTED_VALUE);
122123
});
124+
it('Should deal with hapi v16+ URL objects', () => {
125+
const PATH = '/foo/bar';
126+
const REQUEST = {
127+
...FULL_REQ_DERIVATION_VALUE,
128+
url: new URL(`https://www.SUPER-TEST.com${PATH}`)
129+
};
130+
const EXPECTED = {...FULL_REQ_EXPECTED_VALUE, url: PATH};
131+
deepStrictEqual(
132+
hapiRequestInformationExtractor(REQUEST as {} as hapi.Request),
133+
EXPECTED);
134+
});
123135
});
124136
});

0 commit comments

Comments
 (0)