export type Jsonable =
  | string
  | number
  | boolean
  | null
  | undefined
  | readonly Jsonable[]
  | { readonly [key: string]: Jsonable }
  | { toJSON(): Jsonable }

/**
 * Handle errors
 * Wraps the errors without message property and stringify to a message so we can use it later
 * @param error
 * @returns
 */
export function handleError(error: unknown): Error {
  if (error instanceof Error) return error

  // No message error, wrap it and stringify
  let stringified = 'Unable to stringify the thrown value'
  try {
    stringified = JSON.stringify(error)
  } catch (error) {
    console.error(stringified, error)
  }

  return new Error(`Wrapped Error: ${stringified}`)
}
