Skip to content

Commit

Permalink
feat(canary): better error handling (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasveesam authored Dec 10, 2024
1 parent c7fe3a7 commit c8b1e83
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
15 changes: 8 additions & 7 deletions projects/Canary/ClientApp/src/error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { toast } from 'react-semantic-toasts';

export function connectionErrorToast(error) {
toast({
type: 'error',
icon: 'exclamation circle',
title: 'Error!',
description: 'There was an error communicating with Canary. The error was: "' + error + '"',
time: 5000,
});
let errMsg = error?.response?.data?.errorDetails ?? error?.response ?? error
toast({
type: 'error',
icon: 'exclamation circle',
title: 'Error!',
description: 'Error: "' + errMsg + '"',
time: 10000,
});
}
26 changes: 26 additions & 0 deletions projects/Canary/Filter/ExceptionHandlingFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using System;

namespace canary.Filter
{
public class ExceptionHandlingFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
var exception = context.Exception;

var errorResponse = new
{
ErrorDetails = exception.Message
};

context.Result = new JsonResult(errorResponse)
{
StatusCode = 500
};

context.ExceptionHandled = true;
}
}
}
6 changes: 5 additions & 1 deletion projects/Canary/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using canary.Filter;

namespace canary
{
Expand Down Expand Up @@ -41,7 +42,10 @@ public void ConfigureServices(IServiceCollection services)
options.SuppressModelStateInvalidFilter = true;
});

services.AddControllers().AddNewtonsoftJson();
services.AddControllers(options =>
{
options.Filters.Add<ExceptionHandlingFilter>();
}).AddNewtonsoftJson();

services.AddCors();

Expand Down

0 comments on commit c8b1e83

Please sign in to comment.