public void When_JSON_only_contains_errorCode()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire""}");

            Assert.AreEqual("ServerCaughtFire", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(0, err.InnerErrors.Count);
        }
        public void When_JSON_contains_errorCode_and_errorDetails()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire"",""errorDetails"":{""in"":""body"",""at"":""some.prop.path""}}");

            Assert.AreEqual("ServerCaughtFire", err.ErrorCode);
            Assert.AreEqual(JObject.Parse(@"{""in"":""body"",""at"":""some.prop.path""}").ToString(), err.RawErrorDetails);
            Assert.AreEqual(0, err.InnerErrors.Count);
        }
Exemplo n.º 3
0
        private async Task ThrowIfPostMarkupBurnersError(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                ErrorData err = await ErrorData.From(response);

                throw new RestApiErrorException(err);
            }
        }
        public void Results_without_a_parsable_inner_errorCode_are_not_considered_inner_errors()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "wat", @"{""errorCode"":""CouldNotDoTheThing"",""output"":{""results"":[{""wat"":""foo""},{""errorCode"":""FailedToExtract""},{""errorCode"":[1,2,3]}]}}");

            Assert.AreEqual("CouldNotDoTheThing", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(1, err.InnerErrors.Count);
            Assert.AreEqual("FailedToExtract", err.InnerErrors[0].ErrorCode);
            Assert.IsNull(err.InnerErrors[0].RawErrorDetails);
        }
        public void When_JSON_contains_errorCode_and_one_result_with_an_inner_errorCode()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "wat", @"{""errorCode"":""CouldNotDoTheThing"",""output"":{""results"":[{""errorCode"":""ReallyBadProblemHappened""}]}}");

            Assert.AreEqual("CouldNotDoTheThing", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(1, err.InnerErrors.Count);
            Assert.AreEqual("ReallyBadProblemHappened", err.InnerErrors[0].ErrorCode);
            Assert.IsNull(err.InnerErrors[0].RawErrorDetails);
        }
        public void When_JSON_contains_errorCode_and_one_result_with_an_inner_errorCode_and_inner_errorDetails()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "wat", @"{""errorCode"":""CouldNotDoTheThing"",""output"":{""results"":[{""errorCode"":""ReallyBadProblemHappened"",""errorDetails"":{""causedBy"":""really-bad-server"",""misbehaving"":true}}]}}");

            Assert.AreEqual("CouldNotDoTheThing", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(1, err.InnerErrors.Count);
            Assert.AreEqual("ReallyBadProblemHappened", err.InnerErrors[0].ErrorCode);
            Assert.AreEqual(JObject.Parse(@"{""causedBy"":""really-bad-server"",""misbehaving"":true}").ToString(), err.InnerErrors[0].RawErrorDetails);
        }
Exemplo n.º 7
0
        public void Constructing_from_ErrorData_correctly_sets_properties_when_there_are_no_errorDetails()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire""}");

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.AreEqual("ServerCaughtFire", exception.ErrorCode);
            Assert.IsNull(exception.RawErrorDetails);
        }
Exemplo n.º 8
0
        public void Constructing_from_ErrorData_correctly_sets_properties_when_there_is_no_body()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", null);

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.IsNull(exception.ErrorCode);
            Assert.IsNull(exception.RawErrorDetails);
        }
Exemplo n.º 9
0
        public void Constructing_from_ErrorData_correctly_sets_properties()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire"",""errorDetails"":{""in"":""body"",""at"":""foo""}}");

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.AreEqual("ServerCaughtFire", exception.ErrorCode);
            Assert.AreEqual(JValue.Parse(@"{""in"":""body"",""at"":""foo""}").ToString(Formatting.Indented), exception.RawErrorDetails);
        }
        public void When_HTTP_status_code_indicates_success_but_the_body_contains_JSON_with_an_errorCode_then_an_ErrorData_instance_is_returned()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "OK", @"{""errorCode"":""WatHappened""}");

            Assert.IsNotNull(err);
            Assert.AreEqual(HttpStatusCode.OK, err.StatusCode);
            Assert.AreEqual("OK", err.ReasonPhrase);
            Assert.AreEqual(@"{""errorCode"":""WatHappened""}", err.RawBody);
            Assert.AreEqual("WatHappened", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(0, err.InnerErrors.Count);
        }
        public void When_JSON_does_not_contain_an_errorCode_but_HTTP_status_code_indicates_an_error_then_an_ErrorData_instance_is_returned()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""firstName"":""Bob""}");

            Assert.IsNotNull(err);
            Assert.AreEqual((HttpStatusCode)480, err.StatusCode);
            Assert.AreEqual("wat", err.ReasonPhrase);
            Assert.AreEqual(@"{""firstName"":""Bob""}", err.RawBody);
            Assert.IsNull(err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(0, err.InnerErrors.Count);
        }
        public void When_JSON_contains_errorCode_and_three_results_two_of_which_contain_inner_error_info()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "wat", @"{""errorCode"":""CouldNotDoTheThing"",""output"":{""results"":[{""errorCode"":""ReallyBadProblemHappened"",""errorDetails"":{""causedBy"":""really-bad-server"",""misbehaving"":true}},{""successfulResult"":true},{""errorCode"":""YetAnotherInnerError""}]}}");

            Assert.AreEqual("CouldNotDoTheThing", err.ErrorCode);
            Assert.IsNull(err.RawErrorDetails);
            Assert.AreEqual(2, err.InnerErrors.Count);
            Assert.AreEqual("ReallyBadProblemHappened", err.InnerErrors[0].ErrorCode);
            Assert.AreEqual(JObject.Parse(@"{""causedBy"":""really-bad-server"",""misbehaving"":true}").ToString(), err.InnerErrors[0].RawErrorDetails);
            Assert.AreEqual("YetAnotherInnerError", err.InnerErrors[1].ErrorCode);
            Assert.IsNull(err.InnerErrors[1].RawErrorDetails);
        }
        public async Task Does_not_extract_body_when_Content_Type_is_not_JSON()
        {
            using (var response = new HttpResponseMessage((HttpStatusCode)418)
            {
                Content = new StringContent("Hello, world!", Encoding.UTF8, "text/plain"),
            })
            {
                ErrorData err = await ErrorData.From(response);

                Assert.AreEqual(err.StatusCode, (HttpStatusCode)418);
                Assert.IsNull(err.RawBody);
            }
        }
        public async Task Extracts_body_when_Content_Type_is_JSON()
        {
            using (var response = new HttpResponseMessage((HttpStatusCode)418)
            {
                Content = new StringContent("\"Hello, world!\"", Encoding.UTF8, "application/json"),
            })
            {
                ErrorData err = await ErrorData.From(response);

                Assert.AreEqual(err.StatusCode, (HttpStatusCode)418);
                Assert.AreEqual(err.RawBody, "\"Hello, world!\"");
            }
        }
        public async Task Correctly_extracts_error_details_from_JSON()
        {
            using (var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                ReasonPhrase = "OK",
                Content = new StringContent(@"{""errorCode"":""CouldNotDoTheThing"",""output"":{""results"":[{""errorCode"":""ReallyBadProblemHappened"",""errorDetails"":{""causedBy"":""really-bad-server"",""misbehaving"":true}}]}}", Encoding.UTF8, "application/json"),
            })
            {
                ErrorData err = await ErrorData.From(response);

                Assert.AreEqual("CouldNotDoTheThing", err.ErrorCode);
                Assert.IsNull(err.RawErrorDetails);
                Assert.AreEqual(1, err.InnerErrors.Count);
                Assert.AreEqual("ReallyBadProblemHappened", err.InnerErrors[0].ErrorCode);
                Assert.AreEqual(JObject.Parse(@"{""causedBy"":""really-bad-server"",""misbehaving"":true}").ToString(), err.InnerErrors[0].RawErrorDetails);
            }
        }
Exemplo n.º 16
0
        private async Task ThrowIfGetRedactionCreatorsError(HttpResponseMessage response)
        {
            ErrorData err = await ErrorData.From(response);

            if (err != null)
            {
                string msg;

                // Example response:
                //
                // {
                //     "processId": "BQa8BGwiY_Ee61ctACsm-w",
                //     "expirationDateTime": "2019-11-13T00:49:43.659Z",
                //     "input": {
                //         "source": {
                //             "fileId": "R-r-mEpIlhzaCf4Lxm1I3g"
                //         },
                //         "rules": [
                //             {
                //                 "find": {
                //                     "type": "regex",
                //                     "pattern": "wat"
                //                 },
                //                 "redactWith": {
                //                     "type": "RectangleRedaction"
                //                 }
                //             }
                //         ]
                //     },
                //     "state": "error",
                //     "percentComplete": 0,
                //     "errorCode": "MarkupCreationError",
                //     "affinityToken": "TD5wTFEBpgocZorewla2epxWbIMOo6GxWhK2oLJn3zo="
                // }
                if (err.ErrorCode == "MarkupCreationError")
                {
                    msg = "The remote server encountered an error when trying to create redactions for the given document. There may be a problem with the document itself.";
                    throw new RestApiErrorException(msg, err);
                }

                // Unknown error
                throw new RestApiErrorException(err);
            }
        }
Exemplo n.º 17
0
        private async Task ThrowIfGetMarkupBurnersError(HttpResponseMessage response)
        {
            ErrorData err = await ErrorData.From(response);

            if (err != null)
            {
                string msg;

                if (err.ErrorCode == "RedactionError")
                {
                    msg = "The remote server was unable to burn the markup file into the document. It is possible there is a problem with the markup JSON or with the document itself.";
                    throw new RestApiErrorException(msg, err);
                }

                if (err.ErrorCode == "InvalidMarkup")
                {
                    msg = "The remote server rejected the given markup JSON because it contained content which did not conform to its allowed markup JSON schema. See the markup JSON schema documentation for your version of PrizmDoc Viewer (such as https://help.accusoft.com/PrizmDoc/latest/HTML/webframe.html#markup-json-specification.html).";
                    throw new RestApiErrorException(msg, err);
                }

                if (err.ErrorCode == "DocumentFileIdDoesNotExist")
                {
                    msg = "Could not use the given RemoteWorkFile as the source document: the work file resource could not be found on the remote server. It may have expired.";
                    throw new RestApiErrorException(msg, err);
                }

                if (err.ErrorCode == "MarkupFileIdDoesNotExist")
                {
                    msg = "Could not use the given RemoteWorkFile as the markup JSON file: the work file resource could not be found on the remote server. It may have expired.";
                    throw new RestApiErrorException(msg, err);
                }

                // Unknown error
                throw new RestApiErrorException(err);
            }
        }
Exemplo n.º 18
0
        private async Task ThrowIfPostRedactionCreatorsError(RedactionMatchRule[] rules, HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                ErrorData err = await ErrorData.From(response);

                string at = this.GetAt(err.RawErrorDetails);
                string msg;

                if (err.ErrorCode == "InvalidInput")
                {
                    int?   ruleIndex       = this.GetRuleIndexFromErrorDetailsAt(at);
                    string ruleDescription = this.GetRuleDescription(rules, ruleIndex);

                    // Example response:
                    //
                    // {
                    //     "in": "body"
                    //     "at": "input.rules[0].redactWith.fontColor",
                    //     "expected": {
                    //         "type": "string"
                    //     },
                    // }
                    if (Regex.Match(at, @"^input\.rules\[\d+\]\.redactWith\.fontColor$").Success)
                    {
                        msg = $"{ruleDescription} has invalid RedactWith.FontColor for remote server: \"{rules.ElementAt(ruleIndex.Value).RedactWith.FontColor}\"";
                        throw new RestApiErrorException(msg, err);
                    }

                    // Example response:
                    //
                    // {
                    //     "in": "body"
                    //     "at": "input.rules[0].redactWith.fillColor",
                    //     "expected": {
                    //         "type": "string"
                    //     },
                    // }
                    if (Regex.Match(at, @"^input\.rules\[\d+\]\.redactWith\.fillColor").Success)
                    {
                        msg = $"{ruleDescription} has invalid RedactWith.FillColor for remote server: \"{rules.ElementAt(ruleIndex.Value).RedactWith.FillColor}\"";
                        throw new RestApiErrorException(msg, err);
                    }

                    // Example response:
                    //
                    // {
                    //     "in": "body"
                    //     "at": "input.rules[0].redactWith.borderColor",
                    //     "expected": {
                    //         "type": "string"
                    //     },
                    // }
                    if (Regex.Match(at, @"^input\.rules\[\d+\]\.redactWith\.borderColor").Success)
                    {
                        msg = $"{ruleDescription} has invalid RedactWith.BorderColor for remote server: \"{rules.ElementAt(ruleIndex.Value).RedactWith.BorderColor}\"";
                        throw new RestApiErrorException(msg, err);
                    }

                    // Example response:
                    //
                    // {
                    //     "errorCode": "InvalidInput",
                    //     "errorDetails": {
                    //         "in": "body",
                    //         "at": "input.rules[0].redactWith.borderThickness",
                    //         "expected": {
                    //             "type": "integer",
                    //             "greaterThan": 0
                    //         }
                    //     }
                    // }
                    if (Regex.Match(at, @"^input\.rules\[\d+\]\.redactWith\.borderThickness").Success)
                    {
                        JToken expected = JObject.Parse(err.RawErrorDetails)["expected"];

                        if ((string)expected["type"] == "integer" && (int)expected["greaterThan"] == 0)
                        {
                            msg = $"{ruleDescription} has invalid RedactWith.BorderThickness for remote server: {rules.ElementAt(ruleIndex.Value).RedactWith.BorderThickness.Value}. Remote server requires a value greater than zero.";
                            throw new RestApiErrorException(msg, err);
                        }
                    }
                }

                // Example response:
                //
                // {
                //     "errorCode": "ResourceNotFound",
                //         "errorDetails": {
                //             "in": "body",
                //             "at": "input.source.fileId"
                //         }
                // }
                if (err.ErrorCode == "ResourceNotFound")
                {
                    if (at == "input.source.fileId")
                    {
                        msg = "Could not use the given RemoteWorkFile as the source document: the work file resource could not be found on the remote server. It may have expired.";
                        throw new RestApiErrorException(msg, err);
                    }
                }

                throw new RestApiErrorException(err);
            }
        }
 public async Task Validates_the_response_is_not_null()
 {
     await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() => await ErrorData.From(null));
 }
Exemplo n.º 20
0
        private async Task ThrowIfPostPlainTextRedactorsError(HttpResponseMessage response, string outputLineEndingFormat)
        {
            if (!response.IsSuccessStatusCode)
            {
                ErrorData err = await ErrorData.From(response);

                if (err != null)
                {
                    string msg;
                    var    at = this.GetAt(err.RawErrorDetails);

                    if (err.ErrorCode == "InvalidInput")
                    {
                        // Example response:
                        //
                        // {
                        //     "errorCode": "InvalidInput",
                        //     "errorDetails": {
                        //         "in": "body",
                        //         "at": "input.dest.lineEndings",
                        //         "expected": {
                        //             "enum": [
                        //                 "\n",
                        //                 "\r\n"]
                        //         }
                        //     }
                        // }
                        if (at == "input.dest.lineEndings")
                        {
                            JToken expected       = JObject.Parse(err.RawErrorDetails)["expected"];
                            string requestedValue = outputLineEndingFormat;
                            IEnumerable <string> supportedValues = ((System.Collections.IEnumerable)expected["enum"]).Cast <dynamic>().Select(x => (string)x);

                            msg = $"Unsupported line ending \"{requestedValue}\". The remote server only supports the following values: {string.Join(", ", supportedValues.Select(x => "\"" + x.Replace("\n", "\\n").Replace("\r", "\\r") + "\"").ToArray())}.";
                            throw new RestApiErrorException(msg, err);
                        }
                    }

                    if (err.ErrorCode == "ResourceNotFound")
                    {
                        // Example response:
                        //
                        // {
                        //     "errorCode": "ResourceNotFound",
                        //     "errorDetails": {
                        //         "in": "body",
                        //         "at": "input.source.fileId"
                        //     }
                        // }
                        if (at == "input.source.fileId")
                        {
                            msg = "Could not use the given RemoteWorkFile as the source document: the work file resource could not be found on the remote server. It may have expired.";
                            throw new RestApiErrorException(msg, err);
                        }

                        // Example response:
                        //
                        // {
                        //     "errorCode": "ResourceNotFound",
                        //     "errorDetails": {
                        //         "in": "body",
                        //         "at": "input.markup.fileId"
                        //     }
                        // }
                        if (at == "input.markup.fileId")
                        {
                            msg = "Could not use the given RemoteWorkFile as the markup JSON file: the work file resource could not be found on the remote server. It may have expired.";
                            throw new RestApiErrorException(msg, err);
                        }
                    }
                }

                // Unknown error
                throw new RestApiErrorException(err);
            }
        }
Exemplo n.º 21
0
        private async Task ThrowIfGetPlainTextRedactorsError(HttpResponseMessage response)
        {
            ErrorData err = await ErrorData.From(response);

            if (err != null)
            {
                string msg;

                // Example response:
                //
                // {
                //     "processId": "UXQ_D1QS0mD0LJD76Q5lSw",
                //     "expirationDateTime": "2019-12-24T20:55:26.086Z",
                //     "input": {
                //         "source": {
                //             "fileId": "VC5vD4x0xSxGF7_rpBfJRw"
                //         },
                //         "markup": {
                //             "fileId": "mhu6HgZOO1QHfxFR-8WEzg"
                //         },
                //         "dest": {
                //             "lineEndings": "\n"
                //         }
                //     },
                //     "state": "error",
                //     "percentComplete": 0,
                //     "errorCode": "InternalError"
                // }
                if (err.ErrorCode == "InternalError")
                {
                    msg = "The remote server was unable to burn the markup file into the document. It is possible there is a problem with the document itself.";
                    throw new RestApiErrorException(msg, err);
                }

                // Example response:
                //
                // {
                //     "processId": "Nef_m5ZCImL9tj6ZQVNh3w",
                //     "expirationDateTime": "2019-12-24T20:57:14.307Z",
                //     "input": {
                //         "source": {
                //             "fileId": "66iz9-y9HybllSPiBZAxkw"
                //         },
                //         "markup": {
                //             "fileId": "Ra04Ge5Hz5_Nanvkv8LdNA"
                //         },
                //         "dest": {
                //             "lineEndings": "\n"
                //         }
                //     },
                //     "state": "error",
                //     "percentComplete": 0,
                //     "errorCode": "InvalidJson"
                // }
                if (err.ErrorCode == "InvalidJson")
                {
                    msg = "The remove server was unable to burn the markup file into the document because the markup file was not valid JSON.";
                    throw new RestApiErrorException(msg, err);
                }

                // Example response:
                //
                // {
                //     "processId": "5K85RfuKzVaCPP7TRGTHtA",
                //     "expirationDateTime": "2019-12-24T21:01:40.293Z",
                //     "input": {
                //         "source": {
                //             "fileId": "66iz9-y9HybllSPiBZAxkw"
                //         },
                //         "markup": {
                //             "fileId": "smxJQ7hK0e_F5KtGWqPD_g"
                //         },
                //         "dest": {
                //             "lineEndings": "\n"
                //         }
                //     },
                //     "state": "error",
                //     "percentComplete": 0,
                //     "errorCode": "InvalidMarkup"
                // }
                if (err.ErrorCode == "InvalidMarkup")
                {
                    msg = "The remote server rejected the given markup JSON because it contained content which did not conform to its allowed markup JSON schema. See the markup JSON schema documentation for your version of PrizmDoc Viewer (such as https://help.accusoft.com/PrizmDoc/latest/HTML/webframe.html#markup-json-specification.html).";
                    throw new RestApiErrorException(msg, err);
                }

                // Unknown error
                throw new RestApiErrorException(err);
            }
        }
        public void When_HTTP_status_code_indicates_success_and_there_is_no_body_then_null_is_returned()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "OK", null);

            Assert.IsNull(err);
        }
        public void When_HTTP_status_code_indicates_success_and_there_is_a_JSON_body_but_no_errorCode_then_null_is_returned()
        {
            ErrorData err = ErrorData.From(HttpStatusCode.OK, "OK", @"{""firstName"":""Bob""}");

            Assert.IsNull(err);
        }