//Type checking here allows us to skip the boilerplate in every catch block.
function errorTypeCheck(passedError: Error | unknown): ParsedError {
  const errorMessage =
    passedError instanceof Error ? passedError.message : String(passedError);
  const errorStack =
    passedError instanceof Error
      ? (passedError.stack ?? "")
      : String(passedError);

  return {
    message: errorMessage,
    stack: errorStack,
  };
}

export default errorTypeCheck;

export interface ParsedError {
  message: string;
  stack: string;
}
