Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
CodeBasedDomainErrorMapper.java
Go to the documentation of this file.
1package dev.rafex.ether.http.core;
2
3/*-
4 * #%L
5 * ether-http-core
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.util.HashMap;
30import java.util.Map;
31import java.util.Objects;
32
33public final class CodeBasedDomainErrorMapper implements DomainErrorMapper {
34
35 private record Key(String operation, String code) {
36 }
37
38 private final Map<Key, HttpError> rules;
39 private final HttpError fallback;
40
41 private CodeBasedDomainErrorMapper(final Map<Key, HttpError> rules, final HttpError fallback) {
42 this.rules = Map.copyOf(rules);
43 this.fallback = fallback;
44 }
45
46 @Override
47 public HttpError map(final String operation, final String code, final Throwable cause) {
48 final var byOperation = rules.get(new Key(normalize(operation), normalize(code)));
49 if (byOperation != null) {
50 return byOperation;
51 }
52 final var byWildcardOp = rules.get(new Key("*", normalize(code)));
53 if (byWildcardOp != null) {
54 return byWildcardOp;
55 }
56 return fallback;
57 }
58
59 public static Builder builder() {
60 return new Builder();
61 }
62
63 private static String normalize(final String value) {
64 return value == null || value.isBlank() ? "*" : value.trim();
65 }
66
67 public static final class Builder {
68
69 private final Map<Key, HttpError> rules = new HashMap<>();
70 private HttpError fallback = new HttpError(400, "domain_error", "domain error");
71
72 public Builder rule(final String operation, final String code, final int status, final String error,
73 final String message) {
74 final var key = new Key(normalize(operation), normalize(code));
75 rules.put(key, new HttpError(status, error, message));
76 return this;
77 }
78
79 public Builder fallback(final int status, final String error, final String message) {
80 fallback = new HttpError(status, error, message);
81 return this;
82 }
83
84 public CodeBasedDomainErrorMapper build() {
85 return new CodeBasedDomainErrorMapper(rules, Objects.requireNonNull(fallback));
86 }
87 }
88}
89
HttpError map(final String operation, final String code, final Throwable cause)
record HttpError(int status, String code, String message)