Skip to main content
Version: Live

PTError

The PTError struct represents a standardized error format used by the PayTheory SDK. It provides a consistent way to communicate errors that may occur during SDK operations.

Properties​

PropertyTypeDescription
codePTErrorCodeAn enumerated error code identifying the type of error.
errorStringA human-readable description of the error.

Initializer​

public init(code: PTErrorCode, error: String)

Creates a new instance of PTError.

ParameterTypeDescription
codePTErrorCodeThe error code for this error.
errorStringA descriptive message explaining the error.

PTErrorCode​

PTErrorCode is an enumeration that represents the possible error codes returned by the PayTheory SDK.

CaseDescription
actionCompleteThe requested action has already been completed.
actionInProgressAn action is currently in progress, preventing a new action.
attestationFailedThe device attestation process failed.
inProgressThe operation is still in progress.
invalidAPIKeyThe provided API key is invalid or not recognized.
invalidParamAn invalid parameter was provided to the SDK.
noFieldsRequired fields were not provided for the operation.
notReadyThe SDK is not in a ready state for the requested operation.
notValidThe provided data failed validation checks.
socketErrorAn error occurred with the WebSocket connection.
tokenFailedToken generation or validation failed.

Usage in SDK​

The PayTheory SDK uses PTError to return errors in a consistent format across various operations. When an error occurs, the SDK will return a PTError instance, allowing developers to easily identify the type of error (via the code property) and get more information about the error (via the error property).

Example: Handling a PTError​

Here's an example of how to handle and parse a PTError returned by the SDK:

func handlePTError(_ error: PTError) {
switch error.code {
case .invalidAPIKey:
print("API Key Error: The provided API key is not valid. Please check your configuration.")
case .socketError:
print("Network Error: \(error.error)")
case .notValid:
print("Validation Error: \(error.error)")
case .tokenFailed:
print("Token Error: \(error.error)")
case .notReady:
print("SDK Not Ready: \(error.error)")
default:
print("An unexpected error occurred: \(error.error)")
}

// Log the error for debugging purposes
logError(code: error.code, message: error.error)
}

// Usage example
let error = PTError(code: .invalidAPIKey, error: "The provided API key has expired.")
handlePTError(error)

In this example, the handlePTError function demonstrates how to work with a PTError returned by the SDK. By switching on the code property, you can determine the specific type of error that occurred and handle it appropriately in your application. The error property provides additional details about what went wrong, which can be used for user feedback or logging.

By using PTError, the PayTheory SDK ensures that all errors are returned in a predictable format, making error handling and debugging more straightforward for developers integrating the SDK.