blob: 548e3e5f41e6f435d1fc64d401e27648395a9934 [file] [log] [blame]
Ray Milkeyfbfd2da2014-05-09 17:30:14 -07001package net.onrc.onos.api.rest;
2
3/**
Ray Milkeya8091b12014-05-16 14:42:47 -07004 * Object to represent an instance of a particular REST error. The error
5 * contains a formatted description which has information about the particular
6 * occurence. Objects of this type are passed back to REST callers if an error
7 * is encountered.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -07008 */
Ray Milkeyfbfd2da2014-05-09 17:30:14 -07009public final class RestError {
10
Ray Milkey6fdd91d2014-07-30 16:27:07 -070011 private final RestErrorCode code;
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070012 private final String summary;
Ray Milkeya8091b12014-05-16 14:42:47 -070013 private final String formattedDescription;
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070014
15 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070016 * Hidden default constructor to force usage of the factory method.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070017 */
Ray Milkeya8091b12014-05-16 14:42:47 -070018 private RestError() {
19 // This is never called, but Java requires these initializations
20 // because the members are final.
Ray Milkey6fdd91d2014-07-30 16:27:07 -070021 code = RestErrorCode.INTENT_ALREADY_EXISTS;
Ray Milkeya8091b12014-05-16 14:42:47 -070022 summary = "";
23 formattedDescription = "";
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070024 }
25
26 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070027 * Constructor to make a new Error. Called by factory method.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070028 *
Ray Milkeya8091b12014-05-16 14:42:47 -070029 * @param code code for the new error
30 * @param summary summary string for the new error
31 * @param formattedDescription formatted full description of the error
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070032 */
Ray Milkey6fdd91d2014-07-30 16:27:07 -070033 private RestError(final RestErrorCode code,
Ray Milkeya8091b12014-05-16 14:42:47 -070034 final String summary,
35 final String formattedDescription) {
36 this.code = code;
37 this.summary = summary;
38 this.formattedDescription = formattedDescription;
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070039 }
40
41 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070042 * Fetch the code for this error.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070043 *
Ray Milkeya8091b12014-05-16 14:42:47 -070044 * @return error code
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070045 */
Ray Milkey6fdd91d2014-07-30 16:27:07 -070046 public RestErrorCode getCode() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070047 return code;
48 }
49
50 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070051 * Fetch the summary for this error.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070052 *
Ray Milkeya8091b12014-05-16 14:42:47 -070053 * @return summary
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070054 */
Ray Milkeya8091b12014-05-16 14:42:47 -070055 public String getSummary() {
56 return summary;
57 }
58
59 /**
60 * Fetch the formatted descritpion for this error.
61 *
62 * @return formatted error
63 */
64 public String getFormattedDescription() {
65 return formattedDescription;
66 }
67
68 /**
69 * Creates an object to represent an instance of a REST error. The
70 * descrption is formatted for this particular instance.
71 *
72 * @param code code of the error in the catalog
73 * @param parameters list of positional parameters to use in formatting
74 * the description
75 * @return new RestError representing this intance
76 */
Ray Milkey6fdd91d2014-07-30 16:27:07 -070077 public static RestError createRestError(final RestErrorCode code,
Ray Milkeya8091b12014-05-16 14:42:47 -070078 final Object... parameters) {
79 final RestErrorCatalogEntry error = RestErrorCatalog.getRestError(code);
80 final String formattedDescription =
81 RestErrorFormatter.formatErrorMessage(error, parameters);
82 return new RestError(code, error.getSummary(), formattedDescription);
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070083 }
84}