package com.amazon.ads.apidemo.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Utility class for triggering debug crashes and dumping exception stack traces.
 */
public class Crash {
    private Crash() {
    }

    /**
     * Manually trigger a crash for debugging purposes.
     * This method intentionally causes a NullPointerException.
     */
    @SuppressWarnings("null")
    public static void crash() {
        String str = null;
        str = str.substring(0, 5);
    }

    /**
     * Dumps the stack trace of a Throwable to a string.
     *
     * @param o The object to dump. If it is a Throwable, its stack trace is returned.
     * @return The stack trace as a string, or an empty string if the object is not a Throwable.
     */
    public static String dump(Object o) {
        if (o instanceof Throwable) {
            Throwable e = (Throwable) o;
            final Writer w = new StringWriter();
            e.printStackTrace(new PrintWriter(w));
            return w.toString();
        }

        return "";
    }
}
