|
4 | 4 | import main.exceptions.AqualityException;
|
5 | 5 | import main.exceptions.AqualityQueryParameterException;
|
6 | 6 | import main.model.dto.DtoMapperGeneral;
|
| 7 | +import main.model.dto.ErrorDto; |
7 | 8 | import org.jetbrains.annotations.NotNull;
|
8 | 9 |
|
9 | 10 | import javax.naming.AuthenticationException;
|
@@ -132,9 +133,10 @@ protected void setOptionsResponseHeaders(@NotNull HttpServletResponse resp) {
|
132 | 133 | resp.setStatus(204);
|
133 | 134 | }
|
134 | 135 |
|
135 |
| - private void setAuthorizationProblem(@NotNull HttpServletResponse resp, @NotNull Exception e) { |
| 136 | + private void setAuthorizationProblem(@NotNull HttpServletResponse resp, @NotNull Exception e) throws AqualityException { |
136 | 137 | resp.setStatus(401);
|
137 | 138 | resp.addHeader("ErrorMessage", !Objects.equals(e.getMessage(), "") ? e.getMessage() : "Are you sure you logged in?");
|
| 139 | + setResponseBody(resp, !Objects.equals(e.getMessage(), "") ? e.getMessage() : "Are you sure you logged in?"); |
138 | 140 | }
|
139 | 141 |
|
140 | 142 | protected void setAuthorizationProblem(@NotNull HttpServletResponse resp) {
|
@@ -191,37 +193,54 @@ protected void processResponse(HttpServletResponse response, String filePath) {
|
191 | 193 | }
|
192 | 194 | }
|
193 | 195 |
|
| 196 | + protected void setResponseBody(HttpServletResponse resp, Object object) throws AqualityException { |
| 197 | + try { |
| 198 | + setJSONContentType(resp); |
| 199 | + resp.getWriter().write(mapper.serialize(object)); |
| 200 | + } catch (IOException e) { |
| 201 | + throw new AqualityException("System was not able to write a response! Raise an Issue."); |
| 202 | + } |
| 203 | + } |
| 204 | + |
194 | 205 | protected void handleException(HttpServletResponse resp, @NotNull Exception e) {
|
195 | 206 | e.printStackTrace();
|
196 |
| - switch (e.getClass().getSimpleName()) { |
197 |
| - case "UnsupportedOperationException": |
198 |
| - setNotImplementedFunction(resp, e); |
199 |
| - return; |
200 |
| - case "AuthenticationException": |
201 |
| - setAuthorizationProblem(resp, e); |
202 |
| - return; |
203 |
| - case "AqualityPermissionsException": |
204 |
| - case "AqualityException": |
205 |
| - case "InvalidFormatException": |
206 |
| - case "AqualityQueryParameterException": |
207 |
| - case "AqualitySQLException": |
208 |
| - AqualityException exception = (AqualityException) e; |
209 |
| - resp.setStatus(exception.getResponseCode()); |
210 |
| - resp.addHeader("ErrorMessage", exception.getMessage()); |
211 |
| - return; |
212 |
| - default: |
213 |
| - setUnknownIssue(resp); |
| 207 | + try { |
| 208 | + switch (e.getClass().getSimpleName()) { |
| 209 | + case "UnsupportedOperationException": |
| 210 | + setNotImplementedFunction(resp, e); |
| 211 | + return; |
| 212 | + case "AuthenticationException": |
| 213 | + setAuthorizationProblem(resp, e); |
| 214 | + return; |
| 215 | + case "AqualityParametersException": |
| 216 | + case "AqualityPermissionsException": |
| 217 | + case "AqualityException": |
| 218 | + case "InvalidFormatException": |
| 219 | + case "AqualityQueryParameterException": |
| 220 | + case "AqualitySQLException": |
| 221 | + AqualityException exception = (AqualityException) e; |
| 222 | + resp.setStatus(exception.getResponseCode()); |
| 223 | + resp.addHeader("ErrorMessage", exception.getMessage()); |
| 224 | + setResponseBody(resp, new ErrorDto(exception.getMessage())); |
| 225 | + return; |
| 226 | + default: |
| 227 | + setUnknownIssue(resp); |
| 228 | + } |
| 229 | + } catch (AqualityException ex) { |
| 230 | + handleException(resp, ex); |
214 | 231 | }
|
215 | 232 | }
|
216 | 233 |
|
217 |
| - private void setNotImplementedFunction(@NotNull HttpServletResponse resp, @NotNull Exception e) { |
| 234 | + private void setNotImplementedFunction(@NotNull HttpServletResponse resp, @NotNull Exception e) throws AqualityException { |
218 | 235 | resp.setStatus(501);
|
219 | 236 | resp.addHeader("ErrorMessage", e.getMessage());
|
| 237 | + setResponseBody(resp, new ErrorDto(e.getMessage())); |
220 | 238 | }
|
221 | 239 |
|
222 |
| - private void setUnknownIssue(@NotNull HttpServletResponse resp) { |
| 240 | + private void setUnknownIssue(@NotNull HttpServletResponse resp) throws AqualityException { |
223 | 241 | resp.setStatus(500);
|
224 | 242 | resp.addHeader("ErrorMessage", "Unknown Issue.");
|
| 243 | + setResponseBody(resp, new ErrorDto("Unknown Issue.")); |
225 | 244 | }
|
226 | 245 |
|
227 | 246 | protected void assertRequiredField(HttpServletRequest request, String fieldName) throws AqualityException {
|
|
0 commit comments