@@ -229,6 +229,7 @@ public extension APIClient {
229229 try configs. requestValidator. validate ( request, configs. with ( \. requestValidator, . alwaysSuccess) )
230230 if !caller. logRequestByItSelf {
231231 configs. logRequest ( request, uuid: uuid)
232+ configs. listener. onRequestStarted ( id: uuid, request: request, configs: configs)
232233 }
233234
234235 return try caller. call ( uuid: uuid, request: request, configs: configs) { response, validate in
@@ -240,6 +241,7 @@ public extension APIClient {
240241 updateTotalResponseMetrics ( for: request, successful: true )
241242 }
242243 #endif
244+ configs. listener. onResponseSerialized ( id: uuid, response: result, configs: configs)
243245 return result
244246 } catch {
245247 #if canImport(Metrics)
@@ -253,37 +255,49 @@ public extension APIClient {
253255 response: response as? Data ,
254256 fileIDLine: fileIDLine
255257 )
256- if let data = response as? Data , let failure = configs. errorDecoder. decodeError ( data, configs) {
257- try configs. errorHandler ( failure, configs, context)
258- throw failure
258+ do {
259+ if let data = response as? Data , let failure = configs. errorDecoder. decodeError ( data, configs) {
260+ try configs. errorHandler ( failure, configs, context)
261+ throw failure
262+ }
263+ try configs. errorHandler ( error, configs, context)
264+ throw error
265+ } catch {
266+ configs. listener. onError ( id: uuid, error: error, configs: configs)
267+ throw error
259268 }
260- try configs. errorHandler ( error, configs, context)
261- throw error
262269 }
263270 }
264271 }
265272 } catch {
266- try withConfigs { configs in
267- let fileIDLine = configs. fileIDLine ?? FileIDLine ( fileID: fileID, line: line)
268- let configs = configs. with ( \. fileIDLine, fileIDLine)
269- if !configs. _errorLoggingComponents. isEmpty {
270- let message = configs. _errorLoggingComponents. errorMessage (
271- uuid: uuid,
272- error: error,
273- maskedHeaders: configs. logMaskedHeaders,
274- fileIDLine: fileIDLine
275- )
276- configs. logger. log ( level: configs. _errorLogLevel, " \( message) " )
273+ do {
274+ try withConfigs { configs in
275+ let fileIDLine = configs. fileIDLine ?? FileIDLine ( fileID: fileID, line: line)
276+ let configs = configs. with ( \. fileIDLine, fileIDLine)
277+ if !configs. _errorLoggingComponents. isEmpty {
278+ let message = configs. _errorLoggingComponents. errorMessage (
279+ uuid: uuid,
280+ error: error,
281+ maskedHeaders: configs. logMaskedHeaders,
282+ fileIDLine: fileIDLine
283+ )
284+ configs. logger. log ( level: configs. _errorLogLevel, " \( message) " )
285+ }
286+ #if canImport(Metrics)
287+ if configs. reportMetrics {
288+ updateTotalErrorsMetrics ( for: nil )
289+ }
290+ #endif
291+ let context = APIErrorContext ( fileIDLine: fileIDLine)
292+ try configs. errorHandler ( error, configs, context)
277293 }
278- #if canImport(Metrics)
279- if configs. reportMetrics {
280- updateTotalErrorsMetrics ( for: nil )
294+ throw error
295+ } catch {
296+ withConfigs { configs in
297+ configs. listener. onError ( id: uuid, error: error, configs: configs)
281298 }
282- #endif
283- let context = APIErrorContext ( fileIDLine: fileIDLine)
284- try configs. errorHandler ( error, configs, context)
299+ throw error
285300 }
286- throw error
287301 }
288302 }
289303}
0 commit comments