|
| 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.external.http.retry; |
| 9 | + |
| 10 | +import org.elasticsearch.core.CheckedFunction; |
| 11 | +import org.elasticsearch.xcontent.ConstructingObjectParser; |
| 12 | +import org.elasticsearch.xcontent.XContentFactory; |
| 13 | +import org.elasticsearch.xcontent.XContentParser; |
| 14 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 15 | +import org.elasticsearch.xcontent.XContentType; |
| 16 | +import org.elasticsearch.xpack.inference.external.http.HttpResult; |
| 17 | + |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.concurrent.Callable; |
| 21 | + |
| 22 | +public class UnifiedChatCompletionErrorResponseUtils { |
| 23 | + |
| 24 | + /** |
| 25 | + * Creates a {@link UnifiedChatCompletionErrorParserContract} that parses the error response as a string. |
| 26 | + * This is useful for cases where the error response is too complicated to parse. |
| 27 | + * |
| 28 | + * @param type The type of the error, used for categorization. |
| 29 | + * @return A {@link UnifiedChatCompletionErrorParserContract} instance. |
| 30 | + */ |
| 31 | + public static UnifiedChatCompletionErrorParserContract createErrorParserWithStringify(String type) { |
| 32 | + return new UnifiedChatCompletionErrorParserContract() { |
| 33 | + @Override |
| 34 | + public UnifiedChatCompletionErrorResponse parse(HttpResult result) { |
| 35 | + try { |
| 36 | + String errorMessage = new String(result.body(), StandardCharsets.UTF_8); |
| 37 | + return new UnifiedChatCompletionErrorResponse(errorMessage, type, null, null); |
| 38 | + } catch (Exception e) { |
| 39 | + // swallow the error |
| 40 | + } |
| 41 | + |
| 42 | + return UnifiedChatCompletionErrorResponse.UNDEFINED_ERROR; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public UnifiedChatCompletionErrorResponse parse(String result) { |
| 47 | + return new UnifiedChatCompletionErrorResponse(result, type, null, null); |
| 48 | + } |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Creates a {@link UnifiedChatCompletionErrorParserContract} that uses a {@link ConstructingObjectParser} to parse the error response. |
| 54 | + * This is useful for cases where the error response can be parsed into an object. |
| 55 | + * |
| 56 | + * @param objectParser The {@link ConstructingObjectParser} to use for parsing the error response. |
| 57 | + * @return A {@link UnifiedChatCompletionErrorParserContract} instance. |
| 58 | + */ |
| 59 | + public static UnifiedChatCompletionErrorParserContract createErrorParserWithObjectParser( |
| 60 | + ConstructingObjectParser<Optional<UnifiedChatCompletionErrorResponse>, Void> objectParser |
| 61 | + ) { |
| 62 | + return new UnifiedChatCompletionErrorParser<>((parser) -> objectParser.apply(parser, null)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a {@link UnifiedChatCompletionErrorParserContract} that uses a generic parser function to parse the error response. |
| 67 | + * This is useful for cases where the error response can be parsed using custom logic, typically when parsing from a map. |
| 68 | + * |
| 69 | + * @param genericParser The function that takes an {@link XContentParser} and returns an |
| 70 | + * {@link Optional<UnifiedChatCompletionErrorResponse>}. |
| 71 | + * @param <E> The type of exception that the parser can throw. |
| 72 | + * @return A {@link UnifiedChatCompletionErrorParserContract} instance. |
| 73 | + */ |
| 74 | + public static <E extends Exception> UnifiedChatCompletionErrorParserContract createErrorParserWithGenericParser( |
| 75 | + CheckedFunction<XContentParser, Optional<UnifiedChatCompletionErrorResponse>, E> genericParser |
| 76 | + ) { |
| 77 | + return new UnifiedChatCompletionErrorParser<>(genericParser); |
| 78 | + } |
| 79 | + |
| 80 | + private record UnifiedChatCompletionErrorParser<E extends Exception>( |
| 81 | + CheckedFunction<XContentParser, Optional<UnifiedChatCompletionErrorResponse>, E> genericParser |
| 82 | + ) implements UnifiedChatCompletionErrorParserContract { |
| 83 | + |
| 84 | + @Override |
| 85 | + public UnifiedChatCompletionErrorResponse parse(HttpResult result) { |
| 86 | + return executeGenericParser(genericParser, createHttpResultXContentParserFunction(result)); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public UnifiedChatCompletionErrorResponse parse(String result) { |
| 91 | + return executeGenericParser(genericParser, createStringXContentParserFunction(result)); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + private static Callable<XContentParser> createHttpResultXContentParserFunction(HttpResult response) { |
| 97 | + return () -> XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, response.body()); |
| 98 | + } |
| 99 | + |
| 100 | + private static Callable<XContentParser> createStringXContentParserFunction(String response) { |
| 101 | + return () -> XContentFactory.xContent(XContentType.JSON).createParser(XContentParserConfiguration.EMPTY, response); |
| 102 | + } |
| 103 | + |
| 104 | + private static <E extends Exception> UnifiedChatCompletionErrorResponse executeGenericParser( |
| 105 | + CheckedFunction<XContentParser, Optional<UnifiedChatCompletionErrorResponse>, E> genericParser, |
| 106 | + Callable<XContentParser> createXContentParser |
| 107 | + ) { |
| 108 | + try (XContentParser parser = createXContentParser.call()) { |
| 109 | + return genericParser.apply(parser).orElse(UnifiedChatCompletionErrorResponse.UNDEFINED_ERROR); |
| 110 | + } catch (Exception e) { |
| 111 | + // swallow the error |
| 112 | + } |
| 113 | + |
| 114 | + return UnifiedChatCompletionErrorResponse.UNDEFINED_ERROR; |
| 115 | + } |
| 116 | + |
| 117 | + private UnifiedChatCompletionErrorResponseUtils() {} |
| 118 | +} |
0 commit comments