/*
 * 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(String src, int pos, String name, String msg) {
		this(src, pos, name, msg, null);
	}

	public ErrorException(String src, int pos, String name, String msg, Throwable cause) {
		super(msg, cause);
		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.source, e.pos, ent.getName(), e.getMessage(), e);
	}

	public ErrorException( Throwable cause ) {
		this("", -1, "", cause.getMessage(), cause);
	}

	@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();
	}

}

@@ -922,16 +922,59 @@
 ntName;%0A
+%09public String keyword;%0A%09public int index;%0A
 %09public 
@@ -988,16 +988,16 @@
 source;%0A
-
 %09public 
@@ -1104,16 +1104,24 @@
 s, name,
+ %22%22, -1,
  msg, nu
@@ -1217,16 +1217,173 @@
 ause) %7B%0A
+%09%09this(src, pos, name, %22%22, -1, msg, cause);%0A%09%7D%0A%0A%09public ErrorException(String src, int pos, String name, String key, int ind, String msg, Throwable cause) %7B%0A
 %09%09super(
@@ -1413,16 +1413,48 @@
 = name;%0A
+%09%09keyword = key;%0A%09%09index = ind;%0A
 %09%09source
@@ -2051,16 +2051,16 @@
 lder();%0A
-
 %09%09sb.app
@@ -2071,16 +2071,172 @@
 entName)
+;%0A%09%09if (!keyword.isEmpty())%0A%09%09%09sb.append(String.format(%22 keyword '%25s'%22, keyword));%0A%09%09if (index %3E 0)%0A%09%09%09sb.append(String.format(%22, index (%25d)%22, index));%0A%09%09sb
 .append(

add properties 'keyword' and 'index' to ErrorException