|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package io.confluent.kafka.schemaregistry.client.rest.entities; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 21 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 22 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 23 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import javax.annotation.Nullable; |
| 27 | + |
| 28 | +/** |
| 29 | + * Generic JSON error message. |
| 30 | + * |
| 31 | + * <p>This is an updated version of the original <code> |
| 32 | + * io.confluent.kafka.schemaregistry.client.rest.entities.ErrorMessage</code> class from the |
| 33 | + * Confluent Schema Registry Client, which has the same structure in Java and is still fully |
| 34 | + * compatible with the client code, but supports parsing the source JSON error message in more |
| 35 | + * formats via Jackson annotations (see {@link #unpackNestedError}). |
| 36 | + * |
| 37 | + * <p>In additional to the original error format: |
| 38 | + * |
| 39 | + * <pre> |
| 40 | + * { |
| 41 | + * "error_code": 400, |
| 42 | + * "message": "..." |
| 43 | + * } |
| 44 | + * </pre> |
| 45 | + * |
| 46 | + * It also supports this format: |
| 47 | + * |
| 48 | + * <pre> |
| 49 | + * { |
| 50 | + * "error: { |
| 51 | + * "code": 400, |
| 52 | + * "message": "..." |
| 53 | + * } |
| 54 | + * } |
| 55 | + * </pre> |
| 56 | + * |
| 57 | + * Since the class is in the same package <code> |
| 58 | + * io.confluent.kafka.schemaregistry.client.rest.entities</code>, pre-pending this library to the |
| 59 | + * classpath will override its implementation from the Schema Registry Client library and allow |
| 60 | + * parsing Managed Kafka style error messages. |
| 61 | + */ |
| 62 | +@Schema(description = "Error message") |
| 63 | +@JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 64 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 65 | +public class ErrorMessage { |
| 66 | + |
| 67 | + private int errorCode = -1; |
| 68 | + private String message = ""; |
| 69 | + |
| 70 | + public ErrorMessage() {} |
| 71 | + |
| 72 | + // Required for compatibility with the original class. |
| 73 | + // @JsonProperty("error_code") and @JsonProperty("message") annotations |
| 74 | + // have been removed to force Jackson always use the default constructor |
| 75 | + // and then set the values via either setErrorCode/setMessage or |
| 76 | + // unpackNestedError methods. |
| 77 | + public ErrorMessage(int errorCode, String message) { |
| 78 | + this.errorCode = errorCode; |
| 79 | + this.message = message; |
| 80 | + } |
| 81 | + |
| 82 | + @Schema(description = "Error code") |
| 83 | + @JsonProperty("error_code") |
| 84 | + public int getErrorCode() { |
| 85 | + return errorCode; |
| 86 | + } |
| 87 | + |
| 88 | + @JsonProperty("error_code") |
| 89 | + public void setErrorCode(int errorCode) { |
| 90 | + this.errorCode = errorCode; |
| 91 | + } |
| 92 | + |
| 93 | + @Schema(description = "Detailed error message") |
| 94 | + @JsonProperty |
| 95 | + public String getMessage() { |
| 96 | + return message; |
| 97 | + } |
| 98 | + |
| 99 | + @JsonProperty |
| 100 | + public void setMessage(String message) { |
| 101 | + this.message = message; |
| 102 | + } |
| 103 | + |
| 104 | + @JsonProperty("error") |
| 105 | + public void unpackNestedError(Map<String, Object> error) { |
| 106 | + Object code = error.get("code"); |
| 107 | + |
| 108 | + if (code != null) { |
| 109 | + try { |
| 110 | + this.errorCode = ((Integer) code); |
| 111 | + } catch (RuntimeException e1) { |
| 112 | + try { |
| 113 | + this.errorCode = Integer.parseInt(code.toString()); |
| 114 | + } catch (RuntimeException e2) { |
| 115 | + // ignore if can't parse the error code as an integer |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + String status = tryGetNonEmptyString(error.get("status")); |
| 121 | + String message = tryGetNonEmptyString(error.get("message")); |
| 122 | + |
| 123 | + if (message != null && status != null) { |
| 124 | + this.message = status + ": " + message; |
| 125 | + } else if (message != null) { |
| 126 | + this.message = message; |
| 127 | + } else if (status != null) { |
| 128 | + this.message = status; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static String tryGetNonEmptyString(@Nullable Object object) { |
| 133 | + if (object != null) { |
| 134 | + String s = object.toString(); |
| 135 | + if (s.length() > 0) { |
| 136 | + return s; |
| 137 | + } |
| 138 | + } |
| 139 | + return ""; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public boolean equals(@Nullable Object o) { |
| 144 | + if (this == o) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + if (o == null || getClass() != o.getClass()) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + ErrorMessage that = (ErrorMessage) o; |
| 151 | + return errorCode == that.errorCode && Objects.equals(message, that.message); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public int hashCode() { |
| 156 | + return Objects.hash(errorCode, message); |
| 157 | + } |
| 158 | +} |
0 commit comments