From e325a5c27e9a3ae3435c7462534f9cda9508e662 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 6 Apr 2019 19:21:59 -0700 Subject: [PATCH] Better response if invalid JSON is received through the API --- src/Sonarr.Http/Exceptions/ApiException.cs | 2 +- src/Sonarr.Http/REST/RestModule.cs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Sonarr.Http/Exceptions/ApiException.cs b/src/Sonarr.Http/Exceptions/ApiException.cs index 0eefd8474..85824ba51 100644 --- a/src/Sonarr.Http/Exceptions/ApiException.cs +++ b/src/Sonarr.Http/Exceptions/ApiException.cs @@ -30,7 +30,7 @@ private static string GetMessage(HttpStatusCode statusCode, object content) if (content != null) { - result = result + " :" + content; + result = $"{result}: {content}"; } return result; diff --git a/src/Sonarr.Http/REST/RestModule.cs b/src/Sonarr.Http/REST/RestModule.cs index 65596501f..7438bc36f 100644 --- a/src/Sonarr.Http/REST/RestModule.cs +++ b/src/Sonarr.Http/REST/RestModule.cs @@ -3,6 +3,7 @@ using System.Linq; using FluentValidation; using Nancy; +using Newtonsoft.Json; using NzbDrone.Core.Datastore; using Sonarr.Http.Extensions; @@ -194,8 +195,16 @@ protected Action UpdateResource protected TResource ReadResourceFromRequest(bool skipValidate = false) { - //TODO: handle when request is null - var resource = Request.Body.FromJson(); + TResource resource; + + try + { + resource = Request.Body.FromJson(); + } + catch (JsonReaderException e) + { + throw new BadRequestException($"Invalid request body. {e.Message}"); + } if (resource == null) {