Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 4 | * 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 Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 8 | */ |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 9 | public final class RestError { |
| 10 | |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame^] | 11 | private final RestErrorCode code; |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 12 | private final String summary; |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 13 | private final String formattedDescription; |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 16 | * Hidden default constructor to force usage of the factory method. |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 17 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 18 | private RestError() { |
| 19 | // This is never called, but Java requires these initializations |
| 20 | // because the members are final. |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame^] | 21 | code = RestErrorCode.INTENT_ALREADY_EXISTS; |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 22 | summary = ""; |
| 23 | formattedDescription = ""; |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 27 | * Constructor to make a new Error. Called by factory method. |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 28 | * |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 29 | * @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 Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 32 | */ |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame^] | 33 | private RestError(final RestErrorCode code, |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 34 | final String summary, |
| 35 | final String formattedDescription) { |
| 36 | this.code = code; |
| 37 | this.summary = summary; |
| 38 | this.formattedDescription = formattedDescription; |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 42 | * Fetch the code for this error. |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 43 | * |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 44 | * @return error code |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 45 | */ |
Ray Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame^] | 46 | public RestErrorCode getCode() { |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 47 | return code; |
| 48 | } |
| 49 | |
| 50 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 51 | * Fetch the summary for this error. |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 52 | * |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 53 | * @return summary |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 54 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 55 | 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 Milkey | 6fdd91d | 2014-07-30 16:27:07 -0700 | [diff] [blame^] | 77 | public static RestError createRestError(final RestErrorCode code, |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 78 | 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 Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 83 | } |
| 84 | } |