Skip to content

Commit

Permalink
#15 Setting statuscode to BadRequest when input is not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-Vidar Schneider authored and Jon-Vidar Schneider committed Mar 5, 2018
1 parent 4684da4 commit 8f0c198
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
16 changes: 16 additions & 0 deletions src/FhirStarter.Bonfire.STU3/Exceptions/ValidateInputException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace FhirStarter.Bonfire.STU3.Exceptions
{
public class ValidateInputException: ArgumentException
{
public ValidateInputException(string message): base(message)
{
}

public ValidateInputException(string message, Exception innerException): base(message, innerException)
{

}
}
}
21 changes: 21 additions & 0 deletions src/FhirStarter.Bonfire.STU3/Exceptions/ValidateOutputException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FhirStarter.Bonfire.STU3.Exceptions
{
public class ValidateOutputException: ArgumentException
{

public ValidateOutputException(string message) : base(message)
{
}

public ValidateOutputException(string message, Exception innerException) : base(message, innerException)
{

}
}
}
2 changes: 2 additions & 0 deletions src/FhirStarter.Bonfire.STU3/FhirStarter.Bonfire.STU3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\ValidateInputException.cs" />
<Compile Include="Exceptions\ValidateOutputException.cs" />
<Compile Include="Filter\AbstractExceptionFilter.cs" />
<Compile Include="Filter\ExceptionFilter.cs" />
<Compile Include="Helper\UrlHandler.cs" />
Expand Down
17 changes: 9 additions & 8 deletions src/FhirStarter.Bonfire.STU3/Filter/AbstractExceptionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Web;
using System.Web.Http.Filters;
using System.Xml.Linq;
using FhirStarter.Bonfire.STU3.Exceptions;
using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Spark.Engine.Core;
Expand All @@ -31,11 +32,8 @@ public override void OnException(HttpActionExecutedContext context)
operationOutcome = serializer.Parse<OperationOutcome>(exceptionMessage);
}
var outCome = operationOutcome ?? GetOperationOutCome(context.Exception);

var xmlSerializer = new FhirXmlSerializer();
var xml = xmlSerializer.SerializeToString(outCome);

// var xml = FhirSerializer.SerializeResourceToXml(outCome);
var internalOutCome = new FhirXmlParser().Parse<OperationOutcome>(xml);
internalOutCome.Issue[0].Diagnostics = context.Exception.StackTrace;
xml = xmlSerializer.SerializeToString(internalOutCome);
Expand All @@ -53,24 +51,27 @@ private static void SetResponseForClient(HttpActionExecutedContext context, Reso
var acceptJson = acceptEntry.Contains(FhirMediaType.HeaderTypeJson);
var jsonSerializer = new FhirJsonSerializer();
var xmlSerializer = new FhirXmlSerializer();
if (acceptJson)
var statusCode = HttpStatusCode.InternalServerError;
if (context.Exception is ValidateInputException)
{
//var json = FhirSerializer.SerializeToJson(outCome);
statusCode = HttpStatusCode.BadRequest;
}
if (acceptJson)
{
var json = jsonSerializer.SerializeToString(outCome);
context.Response = new HttpResponseMessage
{
Content = new StringContent(json, Encoding.UTF8, FhirMediaType.JsonResource),
StatusCode = HttpStatusCode.InternalServerError
StatusCode = statusCode
};
}
else
{
//var xml = FhirSerializer.SerializeToXml(outCome);
var xml = xmlSerializer.SerializeToString(outCome);
context.Response = new HttpResponseMessage
{
Content = new StringContent(xml, Encoding.UTF8, FhirMediaType.XmlResource),
StatusCode = HttpStatusCode.InternalServerError
StatusCode = statusCode
};
}
}
Expand Down
49 changes: 24 additions & 25 deletions src/FhirStarter.Flare.STU3/Controllers/FhirController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Web.Http;
using System.Web.Http.Cors;
using System.Xml.Linq;
using FhirStarter.Bonfire.STU3.Exceptions;
using FhirStarter.Bonfire.STU3.Interface;
using FhirStarter.Bonfire.STU3.Service;
using FhirStarter.Bonfire.STU3.Validation;
Expand Down Expand Up @@ -147,16 +148,10 @@ public HttpResponseMessage Query(string _query)
[HttpPost, Route("{type}")]
public HttpResponseMessage Create(string type, Resource resource)
{
var xmlSerializer = new FhirXmlSerializer();
var xml =xmlSerializer.SerializeToString(resource);
var service = _handler.FindServiceFromList(_fhirServices, _fhirMockupServices, type);

resource = (Resource) ValidateResource(resource);
if (resource is OperationOutcome)
{
return SendResponse(resource);
}
return _handler.ResourceCreate(type, resource, service);
var xmlSerializer = new FhirXmlSerializer();
var service = _handler.FindServiceFromList(_fhirServices, _fhirMockupServices, type);
resource = (Resource) ValidateResource(resource, true);
return resource is OperationOutcome ? SendResponse(resource) : _handler.ResourceCreate(type, resource, service);
}

[HttpPut, Route("{type}/{id}")]
Expand Down Expand Up @@ -187,35 +182,32 @@ private HttpResponseMessage SendResponse(Base resource)
var returnJson = ReturnJson(accept);
if (!(resource is OperationOutcome))
{
resource = ValidateResource((Resource)resource);
}


resource = ValidateResource((Resource)resource, false);
}
StringContent httpContent;
if (!returnJson)
{
var xmlSerializer = new FhirXmlSerializer();

// var xml = FhirSerializer.SerializeToXml(resource);
var xml = xmlSerializer.SerializeToString(resource);
httpContent =
new StringContent(xml, Encoding.UTF8,
FhirMediaType.XmlResource);
GetHttpContent(xmlSerializer.SerializeToString(resource), FhirMediaType.XmlResource);
}
else
{
var jsonSerializer = new FhirJsonSerializer();

httpContent =
// new StringContent(FhirSerializer.SerializeToJson(resource), Encoding.UTF8,
new StringContent(jsonSerializer.SerializeToString(resource), Encoding.UTF8,
FhirMediaType.JsonResource);
GetHttpContent(jsonSerializer.SerializeToString(resource), FhirMediaType.JsonResource);
}
var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = httpContent };
return response;
}

private Base ValidateResource(Resource resource)
private static StringContent GetHttpContent(string serializedValue, string resourceType)
{
return new StringContent(serializedValue, Encoding.UTF8,
resourceType);
}

private Base ValidateResource(Resource resource, bool isInput)
{

if (_profileValidator == null) return resource;
Expand All @@ -230,7 +222,14 @@ private Base ValidateResource(Resource resource)
resource.Meta.ProfileElement[0].Value.Equals(structureDefinition.Url);
if (!found)
{
throw new ArgumentException($"Profile for {resourceName} must be set to: {structureDefinition.Url}");
var message = $"Profile for {resourceName} must be set to: {structureDefinition.Url}";
if (isInput)
{
throw new ValidateInputException(message);
}

throw new ValidateOutputException(message);

}
}

Expand Down

0 comments on commit 8f0c198

Please sign in to comment.