|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchStatusException; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.rest.RestStatus; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | +import org.elasticsearch.xcontent.ToXContent; |
| 15 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 16 | +import org.elasticsearch.xcontent.XContentFactory; |
| 17 | + |
| 18 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 19 | + |
| 20 | +public class InferenceExceptionTests extends ESTestCase { |
| 21 | + public void testWrapException() throws Exception { |
| 22 | + ElasticsearchStatusException cause = new ElasticsearchStatusException("test", RestStatus.BAD_REQUEST); |
| 23 | + InferenceException testException = new InferenceException("test wrapper", cause); |
| 24 | + |
| 25 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 26 | + builder.startObject(); |
| 27 | + testException.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 28 | + builder.endObject(); |
| 29 | + |
| 30 | + assertThat( |
| 31 | + Strings.toString(builder), |
| 32 | + equalTo( |
| 33 | + "{\"type\":\"inference_exception\",\"reason\":\"test wrapper\"," |
| 34 | + + "\"caused_by\":{\"type\":\"status_exception\",\"reason\":\"test\"}}" |
| 35 | + ) |
| 36 | + ); |
| 37 | + assertThat(testException.status(), equalTo(RestStatus.BAD_REQUEST)); |
| 38 | + } |
| 39 | + |
| 40 | + public void testNullCause() throws Exception { |
| 41 | + InferenceException testException = new InferenceException("test exception", null); |
| 42 | + |
| 43 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 44 | + builder.startObject(); |
| 45 | + testException.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 46 | + builder.endObject(); |
| 47 | + |
| 48 | + assertThat(Strings.toString(builder), equalTo("{\"type\":\"inference_exception\",\"reason\":\"test exception\"}")); |
| 49 | + assertThat(testException.status(), equalTo(RestStatus.INTERNAL_SERVER_ERROR)); |
| 50 | + } |
| 51 | +} |
0 commit comments