public void ThrowOnAnyError_MultipleErrors()
        {
            var response = new BatchAnnotateImagesResponse
            {
                Responses =
                {
                    new AnnotateImageResponse {
                        Error = new Rpc.Status{
                            Message = "Boom"
                        }
                    },
                    new AnnotateImageResponse {
                        TextAnnotations =     { new EntityAnnotation{
                                                    Description = "X"
                                                } }
                    },
                    new AnnotateImageResponse {
                        Error = new Rpc.Status{
                            Message = "Bang"
                        }
                    }
                }
            };
            var exception        = Assert.Throws <AggregateException>(() => response.ThrowOnAnyError());
            var nestedExceptions = exception.InnerExceptions.Cast <AnnotateImageException>().ToList();

            Assert.Equal(2, nestedExceptions.Count);
            Assert.Equal("Boom", nestedExceptions[0].Message);
            Assert.Equal("Bang", nestedExceptions[1].Message);
            Assert.Same(response.Responses[0], nestedExceptions[0].Response);
            // response.Responses[1] is skipped as it had no error.
            Assert.Same(response.Responses[2], nestedExceptions[1].Response);
        }
Example #2
0
        public void ThrowOnAnyError()
        {
            // Snippet: ThrowOnAnyError
            Image image = new Image(); // No content or source!
            // Just a single request in this example, but usually BatchAnnotateImages would be
            // used with multiple requests.
            var request = new AnnotateImageRequest
            {
                Image    = image,
                Features = { new Feature {
                                 Type = Feature.Types.Type.SafeSearchDetection
                             } }
            };
            ImageAnnotatorClient client = ImageAnnotatorClient.Create();

            try
            {
                BatchAnnotateImagesResponse response = client.BatchAnnotateImages(new[] { request });
                // ThrowOnAnyError will throw if any individual response in response.Responses
                // contains an error. Other responses may still have useful results.
                // Errors can be detected manually by checking the Error property in each
                // individual response.
                response.ThrowOnAnyError();
            }
            catch (AggregateException e)
            {
                // Because a batch can have multiple errors, the exception thrown is AggregateException.
                // Each inner exception is an AnnotateImageException
                foreach (AnnotateImageException innerException in e.InnerExceptions)
                {
                    Console.WriteLine(innerException.Response.Error);
                }
            }
            // End snippet
        }
        public void ThrowOnAnyError_NoErrors()
        {
            var response = new BatchAnnotateImagesResponse
            {
                Responses =
                {
                    new AnnotateImageResponse {
                        TextAnnotations =     { new EntityAnnotation {
                                                    Description = "X"
                                                } }
                    },
                    new AnnotateImageResponse {
                        LandmarkAnnotations = { new EntityAnnotation {
                                                    Description = "Y"
                                                } }
                    },
                }
            };

            Assert.Same(response, response.ThrowOnAnyError());
        }
        public void ThrowOnAnyError_OneError()
        {
            var response = new BatchAnnotateImagesResponse
            {
                Responses =
                {
                    new AnnotateImageResponse {
                        TextAnnotations =     { new EntityAnnotation{
                                                    Description = "X"
                                                } }
                    },
                    new AnnotateImageResponse {
                        Error = new Rpc.Status{
                            Message = "Bang"
                        }
                    }
                }
            };
            var exception       = Assert.Throws <AggregateException>(() => response.ThrowOnAnyError());
            var nestedException = (AnnotateImageException)exception.InnerExceptions[0];

            Assert.Equal("Bang", nestedException.Message);
            Assert.Same(response.Responses[1], nestedException.Response);
        }