View Javadoc
1   /*
2    * Copyright (c) 2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.services.eansearch.mapper;
19  
20  import org.springframework.stereotype.Service;
21  
22  import feign.Response;
23  import feign.codec.ErrorDecoder;
24  
25  /**
26   * <p>ResponseErrorMapper -- Filters for HTTP Status codes of the API</p>
27   *
28   * <p>The status codes are documented in the
29   * <a href="https://www.ean-search.org/premium/ean-api.html#__RefHeading___Toc147_3132899627">EAN-Search documentation,
30   * Appendix A</a>. This mapper maps them to the runtime exceptions for a better handling.</p>
31   *
32   * @author klenkes74 {@literal <rlichti@kaiserpfalz-edv.de>}
33   * @since 3.0.0  2023-01-17
34   */
35  @Service
36  public class ResponseErrorMapper implements ErrorDecoder {
37      public static final int INVALID_OPERATION = 400;
38      public static final int INVALID_ACCESS_TOKEN = 401;
39      public static final int REQUEST_LIMIT_REACHED = 402;
40      public static final int INVALID_HTTP_METHOD = 405;
41      public static final int RATE_LIMIT_REACHED = 429;
42  
43      @Override
44      public EanSearchException decode(final String methodKey, final Response response) {
45          return switch (response.status()) {
46              case INVALID_OPERATION -> new EanSearchInvalidOperationException();
47              case INVALID_ACCESS_TOKEN -> new EanSearchInvalidAccessTokenException();
48              case REQUEST_LIMIT_REACHED -> new EanSearchRequestLimitReachedException();
49              case INVALID_HTTP_METHOD -> new EanSearchWrongHTTPMethodException();
50              case RATE_LIMIT_REACHED -> new EanSearchTooManyRequestsException();
51              default -> null;
52          };
53  
54      }
55  }