// Path: src/main/java/com/stackify/api/ErrorItem.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class ErrorItem {
// 
//     /**
//      * The error message
//      */
//     @Setter
//     @JsonProperty("Message")
//     private String message;
// 
//     /**
//      * The error's class name
//      */
//     @JsonProperty("ErrorType")
//     private String errorType;
// 
//     /**
//      * The error type code
//      */
//     @JsonProperty("ErrorTypeCode")
//     private String errorTypeCode;
// 
//     /**
//      * Custom data for the error
//      */
//     @JsonProperty("Data")
//     private Map<String, String> data;
// 
//     /**
//      * The method name
//      */
//     @JsonProperty("SourceMethod")
//     private String sourceMethod;
// 
//     /**
//      * The stack trace
//      */
//     @JsonProperty("StackTrace")
//     private List<TraceFrame> stackTrace;
// 
//     /**
//      * The cause of this error
//      */
//     @JsonProperty("InnerError")
//     private ErrorItem innerError;
// 
// }
// 
// Path: src/main/java/com/stackify/api/TraceFrame.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class TraceFrame {
// 
//     /**
//      * The file name
//      */
//     @JsonProperty("CodeFileName")
//     private String codeFileName;
// 
//     /**
//      * The line number
//      */
//     @JsonProperty("LineNum")
//     private Integer lineNum;
// 
//     /**
//      * The method name
//      */
//     @JsonProperty("Method")
//     private String method;
// 
// }


import java.util.ArrayList;
import java.util.List;
import com.stackify.api.ErrorItem;
import com.stackify.api.TraceFrame;
/*
 * Copyright 2013 Stackify
 *
 * 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.stackify.api.common.lang;



/**
 * Utility class for converting a Throwable object to an ErrorItem object
 * 
 * @author Eric Martin
 */
public class Throwables {

	/**
	 * Returns the Throwable's cause chain as a list. The first entry is the Throwable followed by the cause chain.
	 * @param throwable The Throwable
	 * @return The Throwable and its cause chain
	 */
	public static List<Throwable> getCausalChain(final Throwable throwable) {
		if (throwable == null) {
			throw new NullPointerException("Throwable is null");
		}
		
		List<Throwable> causes = new ArrayList<Throwable>();
		causes.add(throwable);
		
		Throwable cause = throwable.getCause();
		
		while ((cause != null) && (!causes.contains(cause))) {
			causes.add(cause);
			cause = cause.getCause();
		}
		
		return causes;
	}
	
	/**
	 * Converts a Throwable to an ErrorItem
	 * @param logMessage The log message (can be null)
	 * @param t The Throwable to be converted
	 * @return The ErrorItem
	 */
// Path: src/main/java/com/stackify/api/ErrorItem.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class ErrorItem {
// 
//     /**
//      * The error message
//      */
//     @Setter
//     @JsonProperty("Message")
//     private String message;
// 
//     /**
//      * The error's class name
//      */
//     @JsonProperty("ErrorType")
//     private String errorType;
// 
//     /**
//      * The error type code
//      */
//     @JsonProperty("ErrorTypeCode")
//     private String errorTypeCode;
// 
//     /**
//      * Custom data for the error
//      */
//     @JsonProperty("Data")
//     private Map<String, String> data;
// 
//     /**
//      * The method name
//      */
//     @JsonProperty("SourceMethod")
//     private String sourceMethod;
// 
//     /**
//      * The stack trace
//      */
//     @JsonProperty("StackTrace")
//     private List<TraceFrame> stackTrace;
// 
//     /**
//      * The cause of this error
//      */
//     @JsonProperty("InnerError")
//     private ErrorItem innerError;
// 
// }
// 
// Path: src/main/java/com/stackify/api/TraceFrame.java
// @AllArgsConstructor
// @NoArgsConstructor
// @Builder(builderClassName = "Builder", builderMethodName = "newBuilder", toBuilder = true)
// @Data
// @JsonInclude(JsonInclude.Include.NON_NULL)
// @JsonIgnoreProperties(ignoreUnknown = true)
// public class TraceFrame {
// 
//     /**
//      * The file name
//      */
//     @JsonProperty("CodeFileName")
//     private String codeFileName;
// 
//     /**
//      * The line number
//      */
//     @JsonProperty("LineNum")
//     private Integer lineNum;
// 
//     /**
//      * The method name
//      */
//     @JsonProperty("Method")
//     private String method;
// 
// }


// Path: src/main/java/com/stackify/api/common/lang/Throwables.java
import java.util.ArrayList;
import java.util.List;
import com.stackify.api.ErrorItem;
import com.stackify.api.TraceFrame;

/*
 * Copyright 2013 Stackify
 *
 * 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.stackify.api.common.lang;



/**
 * Utility class for converting a Throwable object to an ErrorItem object
 * 
 * @author Eric Martin
 */
public class Throwables {

	/**
	 * Returns the Throwable's cause chain as a list. The first entry is the Throwable followed by the cause chain.
	 * @param throwable The Throwable
	 * @return The Throwable and its cause chain
	 */
	public static List<Throwable> getCausalChain(final Throwable throwable) {
		if (throwable == null) {
			throw new NullPointerException("Throwable is null");
		}
		
		List<Throwable> causes = new ArrayList<Throwable>();
		causes.add(throwable);
		
		Throwable cause = throwable.getCause();
		
		while ((cause != null) && (!causes.contains(cause))) {
			causes.add(cause);
			cause = cause.getCause();
		}
		
		return causes;
	}
	
	/**
	 * Converts a Throwable to an ErrorItem
	 * @param logMessage The log message (can be null)
	 * @param t The Throwable to be converted
	 * @return The ErrorItem
	 */
	public static ErrorItem toErrorItem(final String logMessage, final Throwable t) {