package com.atlassian.bitbucket.test;

import com.atlassian.utils.process.ExternalProcess;
import com.atlassian.utils.process.StringProcessHandler;

public class ProcessFailedException extends RuntimeException implements ProcessResult {

    private final String stdErr;
    private final String output;
    private final int exitCode;

    public ProcessFailedException(ExternalProcess process, StringProcessHandler handler) {
        super(buildMessage(process, handler), handler.getException());
        stdErr = handler.getError();
        output = handler.getOutput();
        exitCode = handler.getExitCode();
    }

    private static String buildMessage(ExternalProcess process, StringProcessHandler handler) {
        return process.getCommandLine() + " failed (" + handler.getExitCode() + ") out:" + handler.getOutput() + ": err:" + handler.getError() + ":";
    }

    public String getStdErr() {
        return stdErr;
    }

    public String getStdOut() {
        return output;
    }

    public int getExitCode() {
        return exitCode;
    }

}
