1 /* 2 * This file is part of Waarp Project (named also Waarp or GG). 3 * 4 * Copyright (c) 2019, Waarp SAS, and individual contributors by the @author 5 * tags. See the COPYRIGHT.txt in the distribution for a full listing of 6 * individual contributors. 7 * 8 * All Waarp Project is free software: you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or (at your 11 * option) any later version. 12 * 13 * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY 14 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 15 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * Waarp . If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 package org.waarp.openr66.protocol.http.restv2.errors; 22 23 import com.fasterxml.jackson.databind.node.ObjectNode; 24 import org.waarp.common.json.JsonHandler; 25 26 import java.io.Serializable; 27 import java.util.Locale; 28 import java.util.ResourceBundle; 29 30 /** 31 * This class represents a user error encountered during the processing of a 32 * request. To create a new Error 33 * instance, use the factory method that corresponds to the desired error in the 34 * {@link RestErrors} factory 35 * class. To objectToJson a {@code Error} object as a JSON String to be sent 36 * back, use the 37 * {@code objectToJson} method with the desired {@code Error} object and {@link 38 * Locale}. To objectToJson an 39 * entire list of errors, use the {@code serializeErrors} method instead. 40 */ 41 public class RestError implements Serializable { 42 private static final long serialVersionUID = -166335077846582940L; 43 /** 44 * The name of the property in the {@code restmessages} ResourceBundle 45 * corresponding to the error message. 46 */ 47 private final String msgKey; 48 49 /** 50 * The message arguments (typically field or parameter names) used to give 51 * more context on the cause of the 52 * error. 53 */ 54 private final String[] args; 55 56 /** 57 * The error's code in the REST API specification. 58 */ 59 private final Integer code; 60 61 /** 62 * Creates an object representing the response message to a request which 63 * produced an error 401 - Bad Request. 64 * Should never be called outside of the {@link RestErrors} factory class. 65 * 66 * @param msgKey the message's property name 67 * @param args the message's parameters 68 * @param code the error's code 69 */ 70 RestError(final String msgKey, final String[] args, final int code) { 71 this.msgKey = msgKey; 72 this.args = args; 73 this.code = code; 74 } 75 76 /** 77 * Returns the error as an {@link ObjectNode}. 78 * 79 * @param lang the language of the error message 80 * 81 * @return the serialized RestError object 82 */ 83 public final ObjectNode makeNode(final Locale lang) { 84 final ResourceBundle bundle = 85 ResourceBundle.getBundle("restmessages", lang); 86 final String message = 87 String.format(lang, bundle.getString(msgKey), (Object[]) args); 88 89 final ObjectNode response = JsonHandler.createObjectNode(); 90 response.put("message", message); 91 response.put("errorCode", code); 92 return response; 93 } 94 }