APIError
APIError
APIError describes a request that the Content Delivery API rejected. A fetch(_:), find(_:) or sync(_:syncTypes:then:) call delivers it as the .failure value of its result, and the async form of the call throws it. Use this class to
- read the error message and error code that the API returned
- read the HTTP status code of the response
- find which credential or UID the API rejected
You never create an APIError. Cast the error you receive with as? APIError.
Class-Level Properties
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| errorMessage | String | Read-only | From the response | The error_message that the API returned. |
| errorCode | Int | Read-only | From the response | The error_code that the API returned. |
| errorInfo | APIError.ErrorInfo! | Read-only | From the response | The errors object, by credential or UID. |
| statusCode | Int! | Read/write | From the response | HTTP status code of the response. |
| debugDescription | String | Read-only | The SDK builds it from the values above | Status code, message, code and field errors as one string. |
Class-Level Notes
When you get an APIError.
- The SDK builds an APIError for every response with a status other than 200 whose body contains error_message, error_code and an errors object.
- When the body lacks one of those, the SDK delivers SDKError.unparseableJSON instead, with the body in its data value.
- A failure that never reached the API, such as a lost connection, arrives as the system URLError, not as an APIError.
What errorInfo contains. errorInfo reads four keys of the errors object. Any other key of errors stays out of the SDK objects, so read errorMessage for the full reason.
The Delivery API Errors reference lists the error codes.
ErrorInfo
APIError.ErrorInfo contains the messages of the errors object of the response. Each property is the list of messages for one key, or nil when the response has no such key. The table below lists its properties.
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| apiKey | [String]? | Read-only | nil | Messages under api_key, or under authtoken when api_key is absent. |
| deliveryToken | [String]? | Read-only | nil | Messages under access_token. |
| environment | [String]? | Read-only | nil | Messages under environment. |
| uid | [String]? | Read-only | nil | Messages under uid. |
| debugDescription | String | Read-only | The SDK builds it from the values above | The messages above as one string. |
SDKError
SDKError covers failures that the SDK detects itself. It conforms to Error and CustomDebugStringConvertible, and debugDescription returns a readable message. The table below lists every case.
| Case | When you receive it |
|---|---|
| invalidHTTPResponse(response: URLResponse?) | The system returned neither data nor an error for a request. |
| invalidURL(string: String) | A taxonomy fetch(_:) call received a response with no item. |
| invalidUID(string: String) | A single entry, asset, content type or global field fetch(_:) call received a response with no item. The value is the UID you asked for. |
| unparseableJSON(data: Data?, errorMessage: String) | The API returned a status other than 200 with a body that the SDK cannot read as an APIError. data holds the body. |
| stackError | The SDK does not use this case. |
| cacheError | A cacheOnly call found no cached response, or a cacheThenNetwork call found none for its first result. |
| syncError | A sync response held neither a sync_token nor a pagination_token, or both. |
Method Index
APIError has no public methods. The properties table above lists its values.
Class-Level Snippet
The snippet below fetches an entry and handles an API rejection, an unreadable error body and a transport failure separately.
Tell the kinds of failure apart
import Foundation
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.contentType(uid: "CONTENT_TYPE_UID").entry(uid: "ENTRY_UID")
.fetch { (result: Result<EntryModel, Error>, response: ResponseType) in
switch result {
case .success(let entry):
print(entry.title)
case .failure(let error as APIError):
// The API rejected the request.
print("HTTP \(error.statusCode ?? 0), error \(error.errorCode): \(error.errorMessage)")
if let uidMessages = error.errorInfo?.uid {
print("UID: \(uidMessages.joined(separator: ", "))")
}
case .failure(let error as SDKError):
// For example, invalidUID when the response holds no entry.
print(error.debugDescription)
case .failure(let error as URLError):
// The request did not reach the API, for example with no connection.
print("Network error: \(error.code)")
case .failure(let error):
print(error)
}
}