1 /*******************************************************************************
2 * Copyright 2013 André Rouél and Dominik Seichter
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 package net.sf.qualitytest.exception;
17
18 import javax.annotation.Nullable;
19
20 import net.sf.qualitytest.blueprint.BlueprintSession;
21
22 /**
23 * @author Dominik Seichter
24 */
25 public class BlueprintException extends RuntimeException {
26
27 private static final long serialVersionUID = -7011658424196608479L;
28 private static final String DEFAULT_MESSAGE = "Error during blueprinting.";
29
30 private BlueprintSession session;
31
32 /**
33 * Constructs a {@code BlueprintException} with the default message {@link BlueprintException#DEFAULT_MESSAGE}.
34 */
35 public BlueprintException() {
36 super(DEFAULT_MESSAGE);
37 }
38
39 /**
40 * Constructs a {@code BlueprintException} with a message.
41 *
42 * @param msg
43 * A message describing the error.
44 */
45 public BlueprintException(final String msg) {
46 super(msg);
47 }
48
49 /**
50 * Constructs a {@code BlueprintException} with a message.
51 *
52 * @param msg
53 * A message describing the error.
54 * @param cause
55 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
56 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
57 */
58 public BlueprintException(final String msg, final Throwable cause) {
59 super(msg, cause);
60 }
61
62 /**
63 * Constructs a new exception with the default message {@link BlueprintException#DEFAULT_MESSAGE}.
64 *
65 * @param cause
66 * the cause (which is saved for later retrieval by the {@link Throwable#getCause()} method). (A
67 * {@code null} value is permitted, and indicates that the cause is nonexistent or unknown.)
68 */
69 public BlueprintException(@Nullable final Throwable cause) {
70 super(DEFAULT_MESSAGE, cause);
71 }
72
73 @Override
74 public String getMessage() {
75 final String message = super.getMessage();
76 if (session != null) {
77 final String context = session.getContext();
78 if (!context.isEmpty()) {
79 return message + " " + context;
80 } else {
81 return message;
82 }
83 } else {
84 return message;
85 }
86 }
87
88 /**
89 * Access the {@link BlueprintSession} during which the exception has occurred.
90 *
91 * @return {@code BlueprintSession}
92 */
93 public @Nullable
94 BlueprintSession getSession() {
95 return session;
96 }
97
98 public void setSession(final BlueprintSession session) {
99 this.session = session;
100 }
101 }