Skip to content

Commit

Permalink
Null detection and improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Birkeli committed Nov 6, 2022
1 parent f21908b commit 51fa5fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Altinn.App.Api/Controllers/EventsReceiverController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.Features;
Expand Down Expand Up @@ -49,7 +50,13 @@ public async Task<ActionResult> Post([FromQuery] string code, [FromBody] CloudEv
{
return Unauthorized();
}


if (cloudEventEnvelope.CloudEvent == null)
{
_logger.LogError("CloudEvent is null, unable to process event! Data received: {data}", JsonSerializer.Serialize(cloudEventEnvelope));
return BadRequest();
}

CloudEvent cloudEvent = cloudEventEnvelope.CloudEvent;

IEventHandler eventHandler = _eventHandlerResolver.ResolveEventHandler(cloudEvent.Type);
Expand Down
5 changes: 5 additions & 0 deletions src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public EventHandlerResolver(IEnumerable<IEventHandler> eventHandlers)
/// <inheritDoc/>
public IEventHandler ResolveEventHandler(string eventType)
{
if (eventType == null)
{
return new UnhandledEventHandler();
}

foreach (var handler in _eventHandlers)
{
if (handler.EventType.ToLower() != eventType.ToLower())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UnhandledEventHandler : IEventHandler
/// <inheritdoc/>
public Task<bool> ProcessEvent(CloudEvent cloudEvent)
{
throw new NotImplementedException($"Received unhandled event {cloudEvent.Type} with the following data: {JsonSerializer.Serialize(cloudEvent)}");
throw new NotImplementedException($"Received unhandled event {cloudEvent?.Type} with the following data: {JsonSerializer.Serialize(cloudEvent)}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task Post_NonValidEventType_ShouldReturnBadRequest()
Time = DateTime.Parse("2022-10-13T09:33:46.6330634Z"),
AlternativeSubject = "/person/17858296439"
};
CloudEventEnvelope envelope = new() { CloudEvent = cloudEvent };
CloudEventEnvelope envelope = new() { CloudEvent = null };

var org = "ttd";
var app = "non-existing-app";
Expand Down

0 comments on commit 51fa5fa

Please sign in to comment.