• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / JavaSpiNNaker / 6233274834

19 Sep 2023 08:46AM UTC coverage: 36.409% (-0.6%) from 36.982%
6233274834

Pull #658

github

dkfellows
Merge branch 'master' into java-17
Pull Request #658: Update Java version to 17

1656 of 1656 new or added lines in 260 files covered. (100.0%)

8373 of 22997 relevant lines covered (36.41%)

0.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.76
/SpiNNaker-allocserv/src/main/java/uk/ac/manchester/spinnaker/alloc/web/RequestFailedException.java
1
/*
2
 * Copyright (c) 2021 The University of Manchester
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package uk.ac.manchester.spinnaker.alloc.web;
17

18
import static java.util.Objects.nonNull;
19
import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
20
import static javax.ws.rs.core.Response.noContent;
21
import static javax.ws.rs.core.Response.status;
22
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
23
import static javax.ws.rs.core.Response.Status.GONE;
24
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
25
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
26
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
27
import static javax.ws.rs.core.Response.Status.Family.SERVER_ERROR;
28
import static org.slf4j.LoggerFactory.getLogger;
29

30
import java.io.Serial;
31

32
import javax.ws.rs.WebApplicationException;
33
import javax.ws.rs.core.Response;
34
import javax.ws.rs.core.Response.Status;
35
import javax.ws.rs.ext.ExceptionMapper;
36
import javax.ws.rs.ext.Provider;
37

38
import org.slf4j.Logger;
39
import org.springframework.stereotype.Component;
40

41
/**
42
 * Thrown to indicate various sorts of problems with the service. Very much like
43
 * a {@link WebApplicationException} except with a different handling strategy.
44
 *
45
 * @author Donal Fellows
46
 */
47
public class RequestFailedException extends RuntimeException {
48
        @Serial
49
        private static final long serialVersionUID = -7522760691720854101L;
50

51
        /** The status code. */
52
        private final Status code;
53

54
        /** The response message contents. */
55
        private final String message;
56

57
        /**
58
         * Create an instance.
59
         *
60
         * @param code
61
         *            The status code.
62
         * @param message
63
         *            The response message contents.
64
         * @param cause
65
         *            The cause of the exception.
66
         */
67
        private RequestFailedException(Status code, String message,
68
                        Throwable cause) {
69
                super(message, cause);
×
70
                this.code = code;
×
71
                this.message = message;
×
72
        }
×
73

74
        /**
75
         * Create an instance.
76
         *
77
         * @param code
78
         *            The status code.
79
         * @param message
80
         *            The response message contents.
81
         */
82
        public RequestFailedException(Status code, String message) {
83
                this(code, message, null);
×
84
        }
×
85

86
        /**
87
         * Create an instance that indicates an internal server error.
88
         *
89
         * @param cause
90
         *            The cause of the server error. <em>This will be used as part
91
         *            of the description of the failure.</em>
92
         */
93
        public RequestFailedException(Throwable cause) {
94
                this(INTERNAL_SERVER_ERROR, "unexpected server problem", cause);
×
95
        }
×
96

97
        /**
98
         * Convert this exception to a response.
99
         *
100
         * @return The response that this exception implies.
101
         */
102
        Response toResponse() {
103
                var cause = getCause();
×
104
                if (cause instanceof WebApplicationException waex) {
×
105
                        return waex.getResponse();
×
106
                } else if (cause != null) {
×
107
                        // Be careful about what bits are extracted from message
108
                        var cls = cause.getClass().getName().replaceFirst("^.*[.]", "")
×
109
                                        .replaceAll("Exception", "");
×
110
                        var msg =
111
                                        cause.getMessage() != null ? ": " + cause.getMessage() : "";
×
112
                        return status(code).type(TEXT_PLAIN)
×
113
                                        .entity(message + ": " + cls + msg).build();
×
114
                } else {
115
                        return status(code).type(TEXT_PLAIN).entity(message).build();
×
116
                }
117
        }
118

119
        private void log(Logger log) {
120
                var cause = getCause();
×
121

122
                if (code.getFamily().equals(SERVER_ERROR)) {
×
123
                        if (nonNull(cause)) {
×
124
                                log.error(message, cause);
×
125
                        } else {
126
                                log.error(message);
×
127
                        }
128
                } else {
129
                        if (nonNull(cause)) {
×
130
                                log.debug(message, cause);
×
131
                        } else {
132
                                log.debug(message);
×
133
                        }
134
                }
135
        }
×
136

137
        /** A resource is no longer believed to exist. */
138
        public static class ItsGone extends RequestFailedException {
139
                @Serial
140
                private static final long serialVersionUID = 3774531853141947270L;
141

142
                /**
143
                 * @param message
144
                 *            What message to use to say "its gone away".
145
                 */
146
                public ItsGone(String message) {
147
                        super(GONE, message);
×
148
                }
×
149
        }
150

151
        /** A resource cannot be located. */
152
        public static class NotFound extends RequestFailedException {
153
                @Serial
154
                private static final long serialVersionUID = 5991697173204757030L;
155

156
                /**
157
                 * @param message
158
                 *            What message to use to say "not found".
159
                 */
160
                public NotFound(String message) {
161
                        super(NOT_FOUND, message);
×
162
                }
×
163

164
                /**
165
                 * @param message
166
                 *            What message to use to say "not found".
167
                 * @param cause
168
                 *            Why we are sending the message.
169
                 */
170
                public NotFound(String message, Throwable cause) {
171
                        super(NOT_FOUND, message, cause);
×
172
                }
×
173
        }
174

175
        /** The client provided bad arguments in a request. */
176
        public static class BadArgs extends RequestFailedException {
177
                @Serial
178
                private static final long serialVersionUID = 7916573155067333350L;
179

180
                /**
181
                 * @param message
182
                 *            What message to use to say "bad arguments".
183
                 */
184
                public BadArgs(String message) {
185
                        super(BAD_REQUEST, message, null);
×
186
                }
×
187
        }
188

189
        /** The response is empty. */
190
        public static class EmptyResponse extends RequestFailedException {
191
                @Serial
192
                private static final long serialVersionUID = -2944836034264700912L;
193

194
                /** Create an instance. */
195
                public EmptyResponse() {
196
                        super(NO_CONTENT, "");
×
197
                }
×
198

199
                @Override
200
                Response toResponse() {
201
                        return noContent().build();
×
202
                }
203
        }
204

205
        /**
206
         * Handler for {@link RequestFailedException}.
207
         *
208
         * @author Donal Fellows
209
         * @hidden
210
         */
211
        @Component
212
        @Provider
213
        public static class RequestFailedExceptionMapper
1✔
214
                        implements ExceptionMapper<RequestFailedException> {
215
                private static final Logger log = getLogger(SpallocServiceImpl.class);
1✔
216

217
                @Override
218
                public Response toResponse(RequestFailedException exception) {
219
                        exception.log(log);
×
220
                        return exception.toResponse();
×
221
                }
222
        }
223
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc