|
2 | 2 | * OneSignal |
3 | 3 | * A powerful way to send personalized messages at scale and build effective customer engagement strategies. Learn more at onesignal.com |
4 | 4 | * |
5 | | - * The version of the OpenAPI document: 5.6.0 |
| 5 | + * The version of the OpenAPI document: 5.7.0 |
6 | 6 | * Contact: devrel@onesignal.com |
7 | 7 | * |
8 | 8 | * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). |
|
13 | 13 |
|
14 | 14 | package com.onesignal.client; |
15 | 15 |
|
| 16 | +import com.google.gson.JsonElement; |
| 17 | +import com.google.gson.JsonObject; |
| 18 | +import com.google.gson.JsonParser; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
16 | 22 | import java.util.Map; |
17 | 23 | import java.util.List; |
18 | 24 |
|
@@ -163,4 +169,76 @@ public String getMessage() { |
163 | 169 | return String.format("Message: %s%nHTTP response code: %s%nHTTP response body: %s%nHTTP response headers: %s", |
164 | 170 | super.getMessage(), this.getCode(), this.getResponseBody(), this.getResponseHeaders()); |
165 | 171 | } |
| 172 | + |
| 173 | + /** |
| 174 | + * Get the error messages carried by the response body, normalized to a flat |
| 175 | + * list of strings regardless of which envelope shape the API returned |
| 176 | + * ({@code {"errors": "..."}}, {@code {"errors": ["..."]}}, |
| 177 | + * {@code {"errors": [{"code": ..., "title": ...}]}}, or an object map such as |
| 178 | + * {@code {"errors": {"invalid_aliases": {...}}}}, surfaced as |
| 179 | + * {@code "<key>: <value>"} entries). Returns an empty list when the body is |
| 180 | + * not a recognizable error envelope. The raw body remains available via |
| 181 | + * {@link #getResponseBody()}. |
| 182 | + * |
| 183 | + * @return the normalized error messages |
| 184 | + */ |
| 185 | + public List<String> getErrorMessages() { |
| 186 | + List<String> messages = new ArrayList<String>(); |
| 187 | + if (responseBody == null || responseBody.isEmpty()) { |
| 188 | + return messages; |
| 189 | + } |
| 190 | + |
| 191 | + try { |
| 192 | + JsonElement root = JsonParser.parseString(responseBody); |
| 193 | + if (!root.isJsonObject()) { |
| 194 | + return messages; |
| 195 | + } |
| 196 | + |
| 197 | + JsonElement errors = root.getAsJsonObject().get("errors"); |
| 198 | + if (errors == null || errors.isJsonNull()) { |
| 199 | + return messages; |
| 200 | + } |
| 201 | + |
| 202 | + if (errors.isJsonPrimitive()) { |
| 203 | + if (errors.getAsJsonPrimitive().isString()) { |
| 204 | + messages.add(errors.getAsString()); |
| 205 | + } |
| 206 | + } else if (errors.isJsonArray()) { |
| 207 | + for (JsonElement item : errors.getAsJsonArray()) { |
| 208 | + if (item.isJsonPrimitive()) { |
| 209 | + if (item.getAsJsonPrimitive().isString()) { |
| 210 | + messages.add(item.getAsString()); |
| 211 | + } |
| 212 | + } else if (item.isJsonObject()) { |
| 213 | + JsonObject object = item.getAsJsonObject(); |
| 214 | + JsonElement title = object.get("title"); |
| 215 | + JsonElement code = object.get("code"); |
| 216 | + if (title != null && title.isJsonPrimitive() && title.getAsJsonPrimitive().isString() |
| 217 | + && !title.getAsString().isEmpty()) { |
| 218 | + messages.add(title.getAsString()); |
| 219 | + } else if (code != null && !code.isJsonNull()) { |
| 220 | + messages.add(code.isJsonPrimitive() ? code.getAsString() : code.toString()); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } else if (errors.isJsonObject()) { |
| 225 | + // Object-shaped envelopes (e.g. {"invalid_aliases": {...}}) carry |
| 226 | + // data under arbitrary keys; surface each so it isn't silently |
| 227 | + // dropped. Key order is unspecified, so sort for deterministic output. |
| 228 | + JsonObject object = errors.getAsJsonObject(); |
| 229 | + for (Map.Entry<String, JsonElement> entry : object.entrySet()) { |
| 230 | + JsonElement value = entry.getValue(); |
| 231 | + String rendered = value.isJsonPrimitive() && value.getAsJsonPrimitive().isString() |
| 232 | + ? value.getAsString() |
| 233 | + : value.toString(); |
| 234 | + messages.add(entry.getKey() + ": " + rendered); |
| 235 | + } |
| 236 | + Collections.sort(messages); |
| 237 | + } |
| 238 | + } catch (RuntimeException e) { |
| 239 | + return messages; |
| 240 | + } |
| 241 | + |
| 242 | + return messages; |
| 243 | + } |
166 | 244 | } |
0 commit comments