aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/model/TracedException.java
blob: 38fd8a67587508a84424902fb80c49dd4e9c89b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.semantics.model;

import org.eclipse.emf.ecore.EObject;

public class TracedException extends RuntimeException {
	private final transient EObject sourceElement;

	public TracedException(EObject sourceElement) {
		this.sourceElement = sourceElement;
	}

	public TracedException(EObject sourceElement, String message) {
		super(message);
		this.sourceElement = sourceElement;
	}

	public TracedException(EObject sourceElement, String message, Throwable cause) {
		super(message, cause);
		this.sourceElement = sourceElement;
	}

	public TracedException(EObject sourceElement, Throwable cause) {
		super(cause);
		this.sourceElement = sourceElement;
	}

	public EObject getSourceElement() {
		return sourceElement;
	}

	@Override
	public String getMessage() {
		var message = super.getMessage();
		if (message == null) {
			return "Internal error";
		}
		return message;
	}

	public static TracedException addTrace(EObject sourceElement, Throwable cause) {
		if (cause instanceof TracedException tracedException && tracedException.sourceElement != null) {
			return tracedException;
		}
		return new TracedException(sourceElement, cause);
	}
}