Skip to content

Commit

Permalink
Avoid NullReferenceException on 401 Unauthorized
Browse files Browse the repository at this point in the history
We frequently experience NullReferenceExceptions in our logging registered by the first chance exception handler.

It is caused by `intakeResponse` being not null: Accepted = 0, Errors = null, and then evaluating `intakeResponse.Errors.Count` triggering an error.

Environment:

Unauthorized 401 on https://53b082e7cfeb4d9a9c117371xxxxxxxx.apm.eu-west-1.aws.cloud.es.io/intake/v2/events

The payload returned is:

```json
{"error":"authentication failed"}\n
```

So an improvement might be to also deserialize this JSON format.
  • Loading branch information
monty241 authored Mar 4, 2025
1 parent 84471b8 commit 9a5ba81
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private void ProcessQueueItems(object[] queueItems)
#else
var intakeResponse = _payloadItemSerializer.Deserialize<IntakeResponse>(response.Content.ReadAsStreamAsync().GetAwaiter().GetResult());
#endif
if (intakeResponse.Errors.Count > 0)
if ((intakeResponse?.Errors?.Count ?? 0) > 0)
message = string.Join(", ", intakeResponse.Errors.Select(e => e.Message).Distinct());
}
_logger?.Error()
Expand Down

0 comments on commit 9a5ba81

Please sign in to comment.