@@ -21,7 +21,7 @@ import {
21
21
ClientRequest ,
22
22
IncomingHttpHeaders ,
23
23
} from 'http' ;
24
- import { IgnoreMatcher } from './types' ;
24
+ import { IgnoreMatcher , Err } from './types' ;
25
25
import { AttributeNames } from './enums/AttributeNames' ;
26
26
import * as url from 'url' ;
27
27
@@ -138,26 +138,40 @@ export class Utils {
138
138
* @param obj to subscribe on error
139
139
*/
140
140
static setSpanOnError ( span : Span , obj : IncomingMessage | ClientRequest ) {
141
- obj . on ( 'error' , error => {
142
- span . setAttributes ( {
143
- [ AttributeNames . HTTP_ERROR_NAME ] : error . name ,
144
- [ AttributeNames . HTTP_ERROR_MESSAGE ] : error . message ,
145
- } ) ;
146
-
147
- let status : Status ;
148
- if ( ( obj as IncomingMessage ) . statusCode ) {
149
- status = Utils . parseResponseStatus (
150
- ( obj as IncomingMessage ) . statusCode !
151
- ) ;
152
- } else {
153
- status = { code : CanonicalCode . UNKNOWN } ;
154
- }
141
+ obj . on ( 'error' , ( error : Err ) => {
142
+ Utils . setSpanWithError ( span , error , obj ) ;
143
+ } ) ;
144
+ }
155
145
156
- status . message = error . message ;
146
+ /**
147
+ * Sets the span with the error passed in params
148
+ * @param {Span } span the span that need to be set
149
+ * @param {Error } error error that will be set to span
150
+ * @param {(IncomingMessage | ClientRequest) } [obj] used for enriching the status by checking the statusCode.
151
+ */
152
+ static setSpanWithError (
153
+ span : Span ,
154
+ error : Err ,
155
+ obj ?: IncomingMessage | ClientRequest
156
+ ) {
157
+ const message = error . message ;
157
158
158
- span . setStatus ( status ) ;
159
- span . end ( ) ;
159
+ span . setAttributes ( {
160
+ [ AttributeNames . HTTP_ERROR_NAME ] : error . name ,
161
+ [ AttributeNames . HTTP_ERROR_MESSAGE ] : message ,
160
162
} ) ;
163
+
164
+ let status : Status ;
165
+ if ( obj && ( obj as IncomingMessage ) . statusCode ) {
166
+ status = Utils . parseResponseStatus ( ( obj as IncomingMessage ) . statusCode ! ) ;
167
+ } else {
168
+ status = { code : CanonicalCode . UNKNOWN } ;
169
+ }
170
+
171
+ status . message = message ;
172
+
173
+ span . setStatus ( status ) ;
174
+ span . end ( ) ;
161
175
}
162
176
163
177
/**
@@ -172,7 +186,6 @@ export class Utils {
172
186
let pathname = '/' ;
173
187
let origin = '' ;
174
188
let optionsParsed : url . URL | url . UrlWithStringQuery | RequestOptions ;
175
-
176
189
if ( typeof options === 'string' ) {
177
190
optionsParsed = url . parse ( options ) ;
178
191
pathname = ( optionsParsed as url . UrlWithStringQuery ) . pathname || '/' ;
@@ -181,15 +194,13 @@ export class Utils {
181
194
Object . assign ( optionsParsed , extraOptions ) ;
182
195
}
183
196
} else {
184
- optionsParsed = options ;
185
- try {
186
- pathname = ( options as url . URL ) . pathname ;
187
- if ( ! pathname && options . path ) {
188
- pathname = url . parse ( options . path ) . pathname || '/' ;
189
- }
190
- origin = `${ options . protocol || 'http:' } //${ options . host ||
191
- `${ options . hostname } :${ options . port } ` } `;
192
- } catch ( ignore ) { }
197
+ optionsParsed = options as RequestOptions ;
198
+ pathname = ( options as url . URL ) . pathname ;
199
+ if ( ! pathname && optionsParsed . path ) {
200
+ pathname = url . parse ( optionsParsed . path ) . pathname || '/' ;
201
+ }
202
+ origin = `${ optionsParsed . protocol || 'http:' } //${ optionsParsed . host ||
203
+ `${ optionsParsed . hostname } :${ optionsParsed . port } ` } `;
193
204
}
194
205
195
206
if ( Utils . hasExpectHeader ( optionsParsed ) ) {
@@ -207,4 +218,17 @@ export class Utils {
207
218
208
219
return { origin, pathname, method, optionsParsed } ;
209
220
}
221
+
222
+ /**
223
+ * Makes sure options is of type string or object
224
+ * @param options for the request
225
+ */
226
+ static isValidOptionsType ( options : unknown ) : boolean {
227
+ if ( ! options ) {
228
+ return false ;
229
+ }
230
+
231
+ const type = typeof options ;
232
+ return type === 'string' || ( type === 'object' && ! Array . isArray ( options ) ) ;
233
+ }
210
234
}
0 commit comments