<?php
namespace nur\sery\web\curl;

use nur\sery\UserException;
use Throwable;

class CurlException extends UserException {
  function __construct($ch, ?string $message=null, $code=0, ?Throwable $previous=null) {
    if ($message === null) $message = "(unknown error)";
    $userMessage = $message;
    $techMessage = null;
    if ($ch !== null) {
      $parts = [];
      $errno = curl_errno($ch);
      if ($errno != 0) $parts[] = "errno: $errno";
      $error = curl_error($ch);
      if ($error != "") $parts[] = "error: $error";
      if ($parts) $techMessage = implode(", ", $parts);
    }
    parent::__construct($userMessage, $techMessage, $code, $previous);
  }
}
