Skip to main content

Exceptions

Every error raised by psxdata inherits from PSXDataError, so you can write a single catch-all handler regardless of what goes wrong:
Catch a more specific subclass when you want to handle network failures differently from bad tickers, or retry on rate limits.

Exception hierarchy


PSXDataError

Base exception for all psxdata library errors. Inherits from the built-in Exception. Catch PSXDataError when you want a single handler that covers every failure the library can raise. All exceptions listed below are subclasses of this class. Raised by: every public function in psxdata under error conditions.

PSXUnavailableError

The PSX server is unreachable or returned a 5xx response. This is the parent of PSXConnectionError and PSXServerError. Catch this class when you want to handle all availability failures with a single branch, without distinguishing between a network error and a server error. Raised by: all functions that make HTTP requests — stocks(), quote(), tickers(), indices(), sectors(), symbols(), fundamentals(), debt_market(), eligible_scrips().

PSXConnectionError

Network-level failure — DNS resolution failed, the connection was refused, or the request timed out. The PSX server was never reached. This error occurs before any HTTP response is received. Raised by: all functions that make HTTP requests, after retries are exhausted. Common causes:
  • No internet connectivity
  • DNS resolution failure for dps.psx.com.pk
  • Connection timeout (server not responding within the request timeout)
  • Connection refused

PSXServerError

The PSX server was reached but returned an HTTP 5xx response (500, 502, 503, etc.), indicating a server-side failure. The request was well-formed; PSX is having trouble on their end. Raised by: all functions that make HTTP requests, after retries are exhausted.

PSXAuthError

PSX returned an HTTP 401 (Unauthorized) or 403 (Forbidden) response. This indicates an authentication or authorisation failure. Raised by: all functions that make HTTP requests, immediately without retry. This error is uncommon for the public PSX data endpoints, but may occur if the PSX website changes access requirements or if a session token becomes invalid.

PSXRateLimitError

PSX returned an HTTP 429 (Too Many Requests) response — rate limit exceeded. The library does not retry automatically on 429. Raised by: all functions that make HTTP requests, immediately without retry. Back off and retry after a delay when catching this exception. The built-in disk cache reduces request frequency significantly — ensure cache=True (the default) where possible.

PSXParseError

The HTML structure of the PSX response changed, or the response could not be parsed into the expected format. Also raised for unexpected 4xx HTTP responses (400, 404, etc.), including requests for index names that do not exist on PSX. Raised by: all functions that make HTTP requests; most commonly surfaced by tickers(index=...) and indices() when passed an invalid index name.
If you receive this error with a known-good index name, it may indicate that PSX has changed the structure of their HTML. Open an issue on the psxdata repository with the index name and error message.

InvalidSymbolError

The requested ticker symbol does not exist on PSX. This is raised when PSX has no record of the symbol — the ticker was never listed, or the symbol string is malformed. DelistedSymbolError is a subclass of this exception, so catching InvalidSymbolError also catches delisted tickers. Raised by: functions that fetch per-symbol data, most commonly stocks().

DelistedSymbolError

The requested ticker symbol existed on PSX but has since been delisted. Because this is a subclass of InvalidSymbolError, catching InvalidSymbolError is sufficient if you do not need to distinguish between a never-listed symbol and a delisted one. Raised by: functions that fetch per-symbol data when a symbol is recognised as a former PSX listing.

DataNotAvailableError

The symbol is valid and listed on PSX, but PSX returned no data for the requested period. This can happen when requesting data for a date range before the company’s IPO, or for a period during which the stock was suspended. Raised by: functions that fetch per-symbol time-series data. This is distinct from InvalidSymbolError: the ticker is real, but no data exists for the given time range.

CacheError

A disk cache read or write operation failed. This covers serialisation failures, deserialisation failures, and underlying diskcache or filesystem errors. Raised by: any public function when cache=True (the default) and the cache directory is inaccessible, the cache is corrupted, or disk space is exhausted. To recover, you can pass cache=False to bypass the cache, or delete the cache directory (default: ~/.psxdata/cache/) and let it be recreated on the next call.