1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
30
31
32
33
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 }