|
| 1 | +/* |
| 2 | + * Copyright Inrupt Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to use, |
| 7 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | + * Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.inrupt.client.examples.webapp; |
| 22 | + |
| 23 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 24 | +import static org.slf4j.LoggerFactory.getLogger; |
| 25 | + |
| 26 | +import com.inrupt.client.solid.DataMappingException; |
| 27 | + |
| 28 | +import io.quarkus.qute.CheckedTemplate; |
| 29 | +import io.quarkus.qute.TemplateInstance; |
| 30 | + |
| 31 | +import jakarta.ws.rs.core.MediaType; |
| 32 | +import jakarta.ws.rs.core.Response; |
| 33 | +import jakarta.ws.rs.ext.ExceptionMapper; |
| 34 | +import jakarta.ws.rs.ext.Provider; |
| 35 | + |
| 36 | +import java.io.ByteArrayInputStream; |
| 37 | +import java.util.Collection; |
| 38 | + |
| 39 | +import org.slf4j.Logger; |
| 40 | + |
| 41 | +@Provider |
| 42 | +public class ClientExceptionMapper implements ExceptionMapper<DataMappingException> { |
| 43 | + |
| 44 | + private static final Logger LOGGER = getLogger(ClientExceptionMapper.class); |
| 45 | + |
| 46 | + @CheckedTemplate |
| 47 | + static class Templates { |
| 48 | + private static native TemplateInstance error( |
| 49 | + String message, |
| 50 | + Collection<String> errors); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Response toResponse(final DataMappingException err) { |
| 55 | + LOGGER.debug("Web Application Error: {}", err.getMessage()); |
| 56 | + |
| 57 | + final var errorHtml = Templates.error("Invalid container", err.getValidationResults()).render(); |
| 58 | + |
| 59 | + final var response = new ByteArrayInputStream(errorHtml.getBytes(UTF_8)); |
| 60 | + |
| 61 | + return Response.status(400) |
| 62 | + .entity(response) |
| 63 | + .type(MediaType.TEXT_HTML) |
| 64 | + .build(); |
| 65 | + } |
| 66 | +} |
0 commit comments