Git Product home page Git Product logo

fumoboy007 / swift-http-error-handling Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 23 KB

Interpret HTTP responses and handle failures in Swift.

Home Page: https://fumoboy007.github.io/swift-http-error-handling/documentation/httperrorhandling/

License: MIT License

Swift 100.00%
async-await error-handling http http-client http-response-status-codes idempotency idempotent-requests retry retry-after retrying

swift-http-error-handling's Introduction

swift-http-error-handling

Interpret HTTP responses and handle failures in Swift.

Swift 5.9 Linux, visionOS 1, macOS 13, iOS 16, tvOS 16, watchOS 9 MIT License Automated Tests Workflow Status

Overview

In the HTTP protocol, a client sends a request to a server and the server sends a response back to the client. The response contains a status code to help the client interpret the response.

HTTP libraries like Foundation pass the response through to the caller without interpreting the response as a success or failure. HTTPErrorHandling can help the caller interpret HTTP responses and handle failures.

The module works with any HTTP library that is compatible with Swift’s standard HTTP request and response types. The module can be used on its own in code that directly uses an HTTP library, or the module can be used as a building block by higher-level networking libraries.

Representing an HTTP Application Failure

After interpreting an HTTP response as a success or a failure, there needs to be a way to represent a failure. In Swift, failures are represented by a type that conforms to the Error protocol. Therefore, the module exposes an HTTPApplicationError type to represent a failure.

Interpreting HTTP Responses

The module extends HTTPResponse with a throwIfFailed method that interprets the response as a success or failure. The method throws HTTPApplicationError if the response is interpreted as a failure.

Some HTTP servers add additional details about a failure to the response body. The throwIfFailed method allows for the response body to be deserialized and attached to the error so that the additional failure details can be accessed later.

Retrying HTTP Requests

The module extends HTTPRequest to add conformance to RetryableRequest, which is a protocol from the swift-retry package that adds safe retry methods to the type. The safe retry methods enforce that the HTTP request is idempotent.

The retry method implementations automatically choose a RecoveryAction for HTTPApplicationError using HTTP-specific information including whether the failure is transient and the value of the Retry-After header, if present.

Example Usage With URLSession

import Foundation
import HTTPErrorHandling
import HTTPTypes
import HTTPTypesFoundation

let request = HTTPRequest(method: .get,
                          scheme: "https",
                          authority: "example.com",
                          path: "/")

let responseBody = try await request.retry { request in
   let (responseBody, response) = try await URLSession.shared.data(for: request)
   try response.throwIfFailed()
   return responseBody
}

See the documentation for more examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.