/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2002-2011 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016-2022 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.basicsim;

import com.jaamsim.input.ExpError;

/**
 * Custom exception thrown when a program error is encountered.
 */
public class ErrorException extends RuntimeException {

	public String entName;
	public String source;
	public int position;

	public ErrorException(int pos, String src, String name, String msg) {
		super(msg);
		entName = name;
		source = src;
		position = pos;
	}

	public ErrorException(String format, Object... args) {
		this(-1, "", "", String.format(format, args));
	}

	public ErrorException(Entity ent, String msg) {
		this(-1, "", ent.getName(), msg);
	}

	public ErrorException(Entity ent, ExpError e) {
		this(e.pos, e.source, ent.getName(), e.getMessage());
	}

	public ErrorException( Throwable cause ) {
		super( cause );
		entName = "";
		source = "";
		position = -1;
	}

	@Override
	public String getMessage() {
		if (entName == null || entName.isEmpty()) {
			return super.getMessage();
		}
		StringBuilder sb = new StringBuilder();
		sb.append(entName).append(": ");
		sb.append(super.getMessage());
		return sb.toString();
	}

}

@@ -1035,24 +1035,151 @@
 ring msg) %7B%0A
+%09%09this(pos, src, name, msg, null);%0A%09%7D%0A%0A%09public ErrorException(int pos, String src, String name, String msg, Throwable cause) %7B%0A
 %09%09super(msg)
@@ -1177,16 +1177,23 @@
 uper(msg
+, cause
 );%0A%09%09ent
@@ -1539,24 +1539,27 @@
 getMessage()
+, e
 );%0A%09%7D%0A%0A%09publ
@@ -1603,70 +1603,51 @@
 %7B%0A%09%09
-super( cause );%0A%09%09entName = %22%22;%0A%09%09source = %22%22;%0A%09%09position = -1
+this(-1, %22%22, %22%22, cause.getMessage(), cause)
 ;%0A%09%7D

set the cause for an ErrorException when it is created from an ExpError