export function getErrorMessage(err: unknown): string {
  if (err instanceof Error) {
    return err.message;
  }

  if (typeof err === "string") {
    return err;
  }

  if (err && typeof err === "object") {
    const withMessage = err as { message?: unknown; error?: unknown };

    if (typeof withMessage.message === "string") {
      return withMessage.message;
    }

    if (typeof withMessage.error === "string") {
      return withMessage.error;
    }

    if (
      withMessage.message &&
      typeof withMessage.message === "object" &&
      "message" in withMessage.message
    ) {
      const nested = withMessage.message as { message?: unknown };
      if (typeof nested.message === "string") {
        return nested.message;
      }
    }

    try {
      return JSON.stringify(err);
    } catch {
      return "Erreur inattendue";
    }
  }

  return String(err ?? "Erreur inattendue");
}
