View Javadoc
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.ArrayNode;
24  import com.fasterxml.jackson.databind.node.ObjectNode;
25  import org.waarp.common.json.JsonHandler;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Locale;
30  
31  /**
32   * Thrown to indicate that the request made to the server is invalid, and lists
33   * all the errors found as a list
34   * of {@link RestError} objects. Typically, these errors will be sent back as a
35   * '400 - Bad Request' HTTP
36   * response.
37   */
38  public class RestErrorException extends RuntimeException {
39  
40    /**
41     *
42     */
43    private static final long serialVersionUID = 6675092325812345581L;
44    /**
45     * The list of all {@link RestError} errors found in the request.
46     */
47    public final List<RestError> errors;
48  
49    /**
50     * Initializes an exception with a single error.
51     *
52     * @param error The error to add.
53     */
54    public RestErrorException(final RestError error) {
55      errors = new ArrayList<RestError>();
56      errors.add(error);
57    }
58  
59    /**
60     * Initializes an exception with a list of errors.
61     *
62     * @param errors The errors to add.
63     */
64    public RestErrorException(final List<RestError> errors) {
65      this.errors = errors;
66    }
67  
68    /**
69     * Returns the exception's list of Error as an {@link ArrayNode} contained
70     * in an {@link ObjectNode}.
71     *
72     * @param lang the language of the error messages.
73     *
74     * @return the serialized list of errors.
75     */
76    public final ObjectNode makeNode(final Locale lang) {
77      final ArrayNode errorsArray = JsonHandler.createArrayNode();
78      for (final RestError error : errors) {
79        errorsArray.add(error.makeNode(lang));
80      }
81      final ObjectNode response = JsonHandler.createObjectNode();
82      response.putArray("errors").addAll(errorsArray);
83  
84      return response;
85    }
86  }