1 /*
2 * Copyright 2008 FatWire Corporation. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.fatwire.gst.foundation;
18
19 import COM.FutureTense.Util.ftErrors;
20
21 /**
22 * Generic Content Server exception that knows about errno.
23 *
24 * @author Dolf Dijkstra
25 * @author Tony Field
26 * @since 10-Jun-2008
27 */
28 public class CSRuntimeException extends RuntimeException {
29 private static final long serialVersionUID = 4188899178173205442L;
30 private final int errno;
31 private final ftErrors complexError;
32
33 /**
34 * @param msg the message
35 * @param errno the Content Server errno
36 */
37 public CSRuntimeException(final String msg, final int errno) {
38 super(msg + " (errno=" + errno + ")");
39 this.errno = errno;
40 this.complexError = null;
41 }
42
43 /**
44 * @param msg the message
45 * @param errno the Content Server errno
46 * @param cause the Throwable as a cause
47 */
48 public CSRuntimeException(final String msg, final int errno, final Throwable cause) {
49 super(msg + " (errno=" + errno + ")", cause);
50 this.errno = errno;
51 this.complexError = null;
52 }
53
54 /**
55 * @param msg the message
56 * @param complexError the complex error
57 * @param errno the Content Server errno
58 */
59 public CSRuntimeException(final String msg, final ftErrors complexError, final int errno) {
60 super(msg, complexError.getCause());
61 this.errno = errno;
62 this.complexError = complexError;
63 }
64
65 /**
66 * @return the Content Server errno
67 */
68 public final int getErrno() {
69 return errno;
70 }
71
72 /**
73 * @return the complex error, or null if it was not set.
74 */
75 public final ftErrors getComplexError() {
76 return complexError;
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see java.lang.Throwable#getMessage()
83 */
84 @Override
85 public String getMessage() {
86 // format:
87 final StringBuilder builder = new StringBuilder();
88 builder.append(super.getMessage());
89 if (complexError != null) {
90 if (errno != complexError.getReason()) {
91 builder.append("|errno:").append(errno);
92 }
93 builder.append("|reason:").append(complexError.getReason());
94 builder.append("|message:");
95 builder.append(complexError.getMessage());
96
97 final int details = complexError.details();
98 if (details > 0) {
99 builder.append("|details:");
100 }
101 for (int i = 0; i < details; i++) {
102 builder.append(" ");
103 builder.append(complexError.detail(i));
104 }
105 } else {
106 builder.append("|errno:").append(errno);
107 }
108 return builder.toString();
109
110 }
111 }