Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

876 B
Raw Blame
import { isAxiosError } from 'axios'

export function httpStatus(error: unknown): number | undefined {
  return isAxiosError(error) ? error.response?.status : undefined
}

/**
 * One line description of an error, for logs that are expected to happen
 * (network blocks, rate limits, offline stores).
 *
 * Passing the raw error to the logger prints a full stack trace, which is
 * useful for a bug but floods the log for a known HTTP failure.
 */
export function summarizeError(error: unknown): string {
  if (!error) return 'unknown error'

  if (isAxiosError(error)) {
    const status = error.response?.status
    const url = error.config?.url
    const suffix = url ? ` (${url})` : ''

    if (status) return `HTTP ${status}${suffix}`
    return `${error.code ?? 'request failed'}${suffix}`
  }

  if (error instanceof Error) return error.message
  return String(error)
}