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.sms77.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 SMS_FAILED_FOR_AT_LEAST_ONE_RECIPIENT = 101;
38      public static final int INVALID_FROM = 201;
39      public static final int INVALID_TO = 202;
40      public static final int NO_TO = 301;
41      public static final int NO_TEXT = 305;
42      public static final int TEXT_TOO_LONG = 401;
43      public static final int RELOAD_DETECTED = 402;
44      public static final int MAX_DAILY_LIMIT_FOR_NUMBER = 403;
45      public static final int NO_CREDITS_LEFT = 500;
46      public static final int CARRIER_TRANSFER_FAILED = 600;
47      public static final int AUTHENTICATION_FAILED = 900;
48      public static final int SIGNATURE_CHECK_FAILED = 901;
49      public static final int API_KEY_NOT_AUTHORIZED = 902;
50      public static final int WRONG_SERVER_IP = 903;
51  
52      @Override
53      public Exception decode(final String methodKey, final Response response) {
54          return switch (response.status()) {
55              case SMS_FAILED_FOR_AT_LEAST_ONE_RECIPIENT -> new Sms77SendingFailedException(101, "At least one recipient did not get the SMS.");
56              case INVALID_FROM -> new Sms77SendingFailedException(201, "Invalid sender given.");
57              case INVALID_TO -> new Sms77SendingFailedException(202, "Invalid recipient given.");
58              case NO_TO -> new Sms77SendingFailedException(301, "No recipient specified.");
59              case NO_TEXT -> new Sms77SendingFailedException(305, "No text specified.");
60              case TEXT_TOO_LONG -> new Sms77SendingFailedException(401, "Text too long.");
61              case CARRIER_TRANSFER_FAILED -> new Sms77SendingFailedException(500, "Carrier transfer failed.");
62  
63              case RELOAD_DETECTED -> new Sms77ReloadException();
64              case MAX_DAILY_LIMIT_FOR_NUMBER -> new Sms77TooManyDailyCallsForThisNumberException();
65              case NO_CREDITS_LEFT -> new Sms77NotEnoughCreditsException();
66  
67              case AUTHENTICATION_FAILED -> new Sms77InvalidAuthException("Invalid API key.");
68              case SIGNATURE_CHECK_FAILED -> new Sms77InvalidAuthException("Signature does not match.");
69              case API_KEY_NOT_AUTHORIZED -> new Sms77InvalidAuthException("API key not valid for this endpoint.");
70              case WRONG_SERVER_IP -> new Sms77InvalidAuthException("Wrong server IP address.");
71  
72              default -> null;
73          };
74      }
75  }