<commit_before>package uk.ac.cam.cl.dtg.teaching.exceptions;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * A class containing exception information which can be serialized to JSON and deserialized again.
 * 
 * @author acr31
 *
 */
public class SerializableException  {

	private String className;

	private String message;

	private SerializableException cause;

	private SerializableStackTraceElement[] stackTrace;
		
	public SerializableException() {
	}

	private static String getHostName() {
		try {
			return Inet4Address.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			throw new Error("Failed to find name for local host",e);
		}
	}
	
	public SerializableException(Throwable toSerialize) {
		this(toSerialize,getHostName());
	}
	
	public SerializableException(Throwable toSerialize,String host) {
		this.message = toSerialize.getMessage();
		this.className = toSerialize.getClass().getName();
		if (toSerialize.getCause() != null) {
			this.cause = new SerializableException(toSerialize.getCause(),host);
		}
		StackTraceElement[] stackTrace = toSerialize.getStackTrace();
		this.stackTrace = new SerializableStackTraceElement[stackTrace.length];
		for (int i = 0; i < stackTrace.length; ++i) {
			this.stackTrace[i] = new SerializableStackTraceElement(stackTrace[i],host);
		}
	}

	SerializableException(
			String className,
			String message,
			SerializableException cause,
			SerializableStackTraceElement[] stackTrace) {
		super();
		this.className = className;
		this.message = message;
		this.cause = cause;
		this.stackTrace = stackTrace;
	}
<commit_msg>Handle the case when getMessage() itself throws an exception<commit_after>package uk.ac.cam.cl.dtg.teaching.exceptions;

import java.net.Inet4Address;
import java.net.UnknownHostException;

/**
 * A class containing exception information which can be serialized to JSON and deserialized again.
 * 
 * @author acr31
 *
 */
public class SerializableException  {
	
	private String className;

	private String message;

	private SerializableException cause;

	private SerializableStackTraceElement[] stackTrace;
		
	public SerializableException() {
	}

	private static String getHostName() {
		try {
			return Inet4Address.getLocalHost().getHostName();
		} catch (UnknownHostException e) {
			throw new Error("Failed to find name for local host",e);
		}
	}
	
	public SerializableException(Throwable toSerialize) {
		this(toSerialize,getHostName());
	}

	public SerializableException(Throwable toSerialize,String host) {
		try {
			this.message = toSerialize.getMessage();
		}
		catch (Throwable e) {
			this.message = toSerialize.getClass().getName()+".getMessage() threw "+e.getClass().getName();
		}
		this.className = toSerialize.getClass().getName();
		if (toSerialize.getCause() != null) {
			this.cause = new SerializableException(toSerialize.getCause(),host);
		}
		StackTraceElement[] stackTrace = toSerialize.getStackTrace();
		this.stackTrace = new SerializableStackTraceElement[stackTrace.length];
		for (int i = 0; i < stackTrace.length; ++i) {
			this.stackTrace[i] = new SerializableStackTraceElement(stackTrace[i],host);
		}
	}

	SerializableException(
			String className,
			String message,
			SerializableException cause,
			SerializableStackTraceElement[] stackTrace) {
		super();
		this.className = className;
		this.message = message;
		this.cause = cause;
		this.stackTrace = stackTrace;
	}

{'fraction_non_alphanumeric': 0.10606060606060606, 'fraction_numerical': 0.002913752913752914, 'mean_word_length': 4.83843537414966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376191', 'n_tokens_mistral': 1076, 'n_tokens_neox': 992, 'n_words': 236}