📄 src/errors.ts
export type FailureReason =
  'not-logged-in' | 'session-expired' | 'network' | 'popup-blocked' | 'server' | 'unknown';

/** An error that should be reported to the user with a Swedish message. */
export class DownloadError extends Error {
  readonly reason: FailureReason;
  readonly detail?: string;

  constructor(reason: FailureReason, detail?: string) {
    super(detail ?? reason);
    this.name = 'DownloadError';
    this.reason = reason;
    this.detail = detail;
  }
}

/** The user closed the loading tab — not an error, just stop quietly. */
export class CancelledError extends Error {
  constructor() {
    super('cancelled by user');
    this.name = 'CancelledError';
  }
}

/** Map any thrown value onto a user-facing failure reason. */
export function toFailureReason(error: unknown): FailureReason {
  if (error instanceof DownloadError) return error.reason;
  if (error instanceof TypeError) return 'network'; // fetch() network failure
  return 'unknown';
}