1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 22 */ 23 package org.opengrok.web.api.error; 24 25 import jakarta.ws.rs.core.MediaType; 26 import jakarta.ws.rs.core.Response; 27 28 public class ExceptionMapperUtils { 29 ExceptionMapperUtils()30 private ExceptionMapperUtils() { 31 } 32 33 /** 34 * Turns the exception into JSON format and embeds it into the response with the provided status. 35 * @param status status of the created response 36 * @param e exception to embed into the response 37 * @return response with the {@code status} and JSON media type with encoded {@code e} in the body 38 */ toResponse(final Response.Status status, final Exception e)39 public static Response toResponse(final Response.Status status, final Exception e) { 40 return Response.status(status) 41 .entity(new ExceptionModel(e.getMessage())) 42 .type(MediaType.APPLICATION_JSON) 43 .build(); 44 } 45 46 private static class ExceptionModel { 47 48 private String message; 49 ExceptionModel(final String message)50 ExceptionModel(final String message) { 51 this.message = message; 52 } 53 getMessage()54 public String getMessage() { 55 return message; 56 } 57 setMessage(final String message)58 public void setMessage(final String message) { 59 this.message = message; 60 } 61 } 62 63 } 64