From 0462dd6be39c031e04495e1fd22c4b0f9b30ba1f Mon Sep 17 00:00:00 2001 From: KevinDockx Date: Tue, 24 Mar 2015 14:22:47 +0100 Subject: [PATCH] [JsonPatch]Rename SimpleObjectAdapter; Include statuscode in JsonPatchException --- ...impleObjectAdapter.cs => ObjectAdapter.cs} | 30 ++++++------- .../TypedJsonPatchDocumentConverter.cs | 4 +- .../Exceptions/JsonPatchException.cs | 25 ++++++++--- .../Exceptions/JsonPatchExceptionBase.cs | 45 +++++++++++++++++++ .../Exceptions/TypedJsonPatchException.cs | 41 ----------------- .../TypedJsonPatchDocument.cs | 2 +- ...tAdapterTests.cs => ObjectAdapterTests.cs} | 2 +- 7 files changed, 84 insertions(+), 65 deletions(-) rename src/Microsoft.AspNet.JsonPatch/Adapters/{SimpleObjectAdapter.cs => ObjectAdapter.cs} (97%) create mode 100644 src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchExceptionBase.cs delete mode 100644 src/Microsoft.AspNet.JsonPatch/Exceptions/TypedJsonPatchException.cs rename test/Microsoft.AspNet.JsonPatch.Test/{SimpleObjectAdapterTests.cs => ObjectAdapterTests.cs} (99%) diff --git a/src/Microsoft.AspNet.JsonPatch/Adapters/SimpleObjectAdapter.cs b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs similarity index 97% rename from src/Microsoft.AspNet.JsonPatch/Adapters/SimpleObjectAdapter.cs rename to src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs index 69aa482a5a..9f95b03541 100644 --- a/src/Microsoft.AspNet.JsonPatch/Adapters/SimpleObjectAdapter.cs +++ b/src/Microsoft.AspNet.JsonPatch/Adapters/ObjectAdapter.cs @@ -12,11 +12,11 @@ using Newtonsoft.Json.Serialization; namespace Microsoft.AspNet.JsonPatch.Adapters { - public class SimpleObjectAdapter : IObjectAdapter where T : class + public class ObjectAdapter : IObjectAdapter where T : class { public IContractResolver ContractResolver { get; set; } - public SimpleObjectAdapter(IContractResolver contractResolver) + public ObjectAdapter(IContractResolver contractResolver) { ContractResolver = contractResolver; } @@ -160,7 +160,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters throw new JsonPatchException(operationToReport, string.Format("Patch failed: provided path is invalid for array property type at " + "location path: {0}: position larger than array size", - path), + path, 422), objectToApplyTo); } } @@ -171,7 +171,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided path is invalid for array property type at location " + "path: {0}: expected array", path), - objectToApplyTo); + objectToApplyTo, 422); } } else @@ -251,7 +251,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location from: {0}: invalid position", operation.from), - objectToApplyTo); + objectToApplyTo, 422); } valueAtFromLocation = array[positionAsInteger]; @@ -262,7 +262,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location from: {0}: expected array", operation.from), - objectToApplyTo); + objectToApplyTo, 422); } } else @@ -360,7 +360,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided path is invalid for array property type at " + "location path: {0}: position larger than array size", path), - objectToApplyTo); + objectToApplyTo, 422); } } } @@ -370,7 +370,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided path is invalid for array property type at " + "location path: {0}: expected array", path), - objectToApplyTo); + objectToApplyTo, 422); } } else @@ -481,7 +481,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location path: {0}: invalid position", operation.path), - objectToApplyTo); + objectToApplyTo, 422); } valueAtPathLocation = array[positionInPathAsInteger]; @@ -492,7 +492,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location path: {0}: expected array", operation.path), - objectToApplyTo); + objectToApplyTo, 422); } } else @@ -600,7 +600,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location from: {0}: invalid position", operation.from), - objectToApplyTo); + objectToApplyTo, 422); } valueAtFromLocation = array[positionAsInteger]; @@ -611,7 +611,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters string.Format("Patch failed: provided from path is invalid for array property type at " + "location from: {0}: expected array", operation.from), - objectToApplyTo); + objectToApplyTo, 422); } } else @@ -636,14 +636,14 @@ namespace Microsoft.AspNet.JsonPatch.Adapters throw new JsonPatchException( operation, string.Format("Patch failed: property at location {0} does not exist", propertyPath), - objectToApplyTo); + objectToApplyTo, 422); } if (patchProperty.Property.Ignored) { throw new JsonPatchException( operation, string.Format("Patch failed: cannot update property at location {0}", propertyPath), - objectToApplyTo); + objectToApplyTo, 422); } } @@ -663,7 +663,7 @@ namespace Microsoft.AspNet.JsonPatch.Adapters var errorMessage = "Patch failed: provided value is invalid for property type at location path: "; if (!result.CanBeConverted) { - throw new JsonPatchException(operation, string.Format(errorMessage + "{0}", path), objectToApplyTo); + throw new JsonPatchException(operation, string.Format(errorMessage + "{0}", path), objectToApplyTo, 422); } } } diff --git a/src/Microsoft.AspNet.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs b/src/Microsoft.AspNet.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs index b4f8f1ef40..8c6f9103b0 100644 --- a/src/Microsoft.AspNet.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs +++ b/src/Microsoft.AspNet.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNet.JsonPatch.Converters { public override bool CanConvert(Type objectType) { - return true; + return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, @@ -60,7 +60,7 @@ namespace Microsoft.AspNet.JsonPatch.Converters } catch (Exception ex) { - throw new JsonPatchException("The JsonPatchDocument was malformed and could not be parsed.", ex); + throw new JsonPatchException("The JsonPatchDocument was malformed and could not be parsed.", ex, 400); } } diff --git a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs index 1ca3f48f0a..d51a0a5d47 100644 --- a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs +++ b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchException.cs @@ -2,14 +2,14 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNet.JsonPatch.Operations; namespace Microsoft.AspNet.JsonPatch.Exceptions { - public class JsonPatchException : Exception + public class JsonPatchException : JsonPatchException where T : class { - public new Exception InnerException { get; internal set; } - - public object AffectedObject { get; private set; } + public Operation FailedOperation { get; private set; } + public new T AffectedObject { get; private set; } private string _message = ""; public override string Message @@ -18,6 +18,7 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions { return _message; } + } public JsonPatchException() @@ -25,9 +26,23 @@ namespace Microsoft.AspNet.JsonPatch.Exceptions } - public JsonPatchException(string message, Exception innerException) + public JsonPatchException(Operation operation, string message, T affectedObject) { + FailedOperation = operation; _message = message; + AffectedObject = affectedObject; + } + + public JsonPatchException(Operation operation, string message, T affectedObject, int statusCode) + : this(operation, message, affectedObject) + { + StatusCode = statusCode; + } + + public JsonPatchException(Operation operation, string message, T affectedObject, + int statusCode, Exception innerException) + : this(operation, message, affectedObject, statusCode) + { InnerException = innerException; } } diff --git a/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchExceptionBase.cs b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchExceptionBase.cs new file mode 100644 index 0000000000..9141a429f0 --- /dev/null +++ b/src/Microsoft.AspNet.JsonPatch/Exceptions/JsonPatchExceptionBase.cs @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; + +namespace Microsoft.AspNet.JsonPatch.Exceptions +{ + public class JsonPatchException : Exception + { + public new Exception InnerException { get; internal set; } + + public int StatusCode { get; internal set; } + + public object AffectedObject { get; private set; } + + private string _message = ""; + public override string Message + { + get + { + return _message; + } + + } + + public JsonPatchException() + { + + } + + public JsonPatchException(string message, Exception innerException) + { + _message = message; + InnerException = innerException; + } + + + public JsonPatchException(string message, Exception innerException, int statusCode) + : this(message, innerException) + { + StatusCode = statusCode; + } + + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.JsonPatch/Exceptions/TypedJsonPatchException.cs b/src/Microsoft.AspNet.JsonPatch/Exceptions/TypedJsonPatchException.cs deleted file mode 100644 index d8e77d4b32..0000000000 --- a/src/Microsoft.AspNet.JsonPatch/Exceptions/TypedJsonPatchException.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using Microsoft.AspNet.JsonPatch.Operations; - -namespace Microsoft.AspNet.JsonPatch.Exceptions -{ - public class JsonPatchException : JsonPatchException where T : class - { - public Operation FailedOperation { get; private set; } - public new T AffectedObject { get; private set; } - - private string _message = ""; - public override string Message - { - get - { - return _message; - } - } - - public JsonPatchException() - { - - } - - public JsonPatchException(Operation operation, string message, T affectedObject) - { - FailedOperation = operation; - _message = message; - AffectedObject = affectedObject; - } - - public JsonPatchException(Operation operation, string message, T affectedObject, Exception innerException) - : this(operation, message, affectedObject) - { - InnerException = innerException; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs b/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs index 812735691a..3417960508 100644 --- a/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs +++ b/src/Microsoft.AspNet.JsonPatch/TypedJsonPatchDocument.cs @@ -354,7 +354,7 @@ namespace Microsoft.AspNet.JsonPatch public void ApplyTo(T objectToApplyTo) { - ApplyTo(objectToApplyTo, new SimpleObjectAdapter(ContractResolver)); + ApplyTo(objectToApplyTo, new ObjectAdapter(ContractResolver)); } public void ApplyTo(T objectToApplyTo, IObjectAdapter adapter) diff --git a/test/Microsoft.AspNet.JsonPatch.Test/SimpleObjectAdapterTests.cs b/test/Microsoft.AspNet.JsonPatch.Test/ObjectAdapterTests.cs similarity index 99% rename from test/Microsoft.AspNet.JsonPatch.Test/SimpleObjectAdapterTests.cs rename to test/Microsoft.AspNet.JsonPatch.Test/ObjectAdapterTests.cs index 1f656b05c0..d4dda66479 100644 --- a/test/Microsoft.AspNet.JsonPatch.Test/SimpleObjectAdapterTests.cs +++ b/test/Microsoft.AspNet.JsonPatch.Test/ObjectAdapterTests.cs @@ -10,7 +10,7 @@ using Xunit; namespace Microsoft.AspNet.JsonPatch.Test { - public class SimpleObjectAdapterTests + public class ObjectAdapterTests { [Fact] public void AddResultsShouldReplace()