RequestError

public struct RequestError : RawRepresentable, Equatable, Hashable, Comparable, Error, CustomStringConvertible

An error representing a failed request. This definition is intended to be used by both the client side (e.g. KituraKit) and server side (e.g. Kitura) of the request (typically a HTTP REST request).

Usage Example:

In this example, the RequestError is used in a Kitura server Codable route handler to indicate the request has failed because the requested record was not found.

router.get("/users") { (id: Int, respondWith: (User?, RequestError?) -> Void) in
    ...
    respondWith(nil, RequestError.notFound)
    ...
}
  • A typealias representing the type of error that has occurred. The range of error codes from 100 up to 599 are reserved for HTTP status codes. Custom error codes may be used and must not conflict with this range.

    Declaration

    Swift

    public typealias RawValue = Int
  • Representation of the error body. May be a type-erased Codable object or a Data (in a particular format).

    See more

    Declaration

    Swift

    public enum ErrorBody

Creating a RequestError from a numeric code

  • Creates an error representing the given error code.

    Declaration

    Swift

    public init(rawValue: Int)

    Parameters

    rawValue

    An Int indicating an error code representing the type of error that has occurred.

  • Creates an error representing the given error code and reason string.

    Declaration

    Swift

    public init(rawValue: Int, reason: String)

    Parameters

    rawValue

    An Int indicating an error code representing the type of error that has occurred.

    reason

    A human-readable description of the error code.

  • Creates an error representing the given base error, with a custom response body given as a Codable.

    Declaration

    Swift

    public init<Body>(_ base: RequestError, body: Body) where Body : Decodable, Body : Encodable

    Parameters

    base

    A RequestError object.

    body

    A representation of the error body - an object representing further details of the failure.

  • Creates an error respresenting the given base error, with a custom response body given as Data and a BodyFormat.

    Throws

    An UnsupportedBodyFormatError if the provided BodyFormat is not supported.

    Declaration

    Swift

    public init(_ base: RequestError, bodyData: Data, format: BodyFormat) throws

    Parameters

    base

    A RequestError object.

    bodyData

    A Data object.

    format

    A BodyFormat object used to check whether it is legal JSON.

Accessing information about the error

  • An error code representing the type of error that has occurred. The range of error codes from 100 up to 599 are reserved for HTTP status codes. Custom error codes may be used and must not conflict with this range.

    Declaration

    Swift

    public let rawValue: Int
  • A human-readable description of the error code.

    Declaration

    Swift

    public let reason: String
  • Representation of the error body - an object representing further details of the failure.

    The value may be:

    • nil if there is no body
    • a (type-erased) Codable object if the error was initialized with init(_:body:)
    • bytes of data and a signifier of the format in which they are stored (eg: JSON) if the error was initialized with init(_:bodyData:format:)

    Usage example:

    if let errorBody = error.body {
        switch error.body {
           case let .codable(body): ... // body is Codable
           case let .data(bytes, format): ... // bytes is Data, format is BodyFormat
        }
    }
    

    Note

    If you need a Codable representation and the body is data, you can call the bodyAs(_:) function to get the converted value.

    Declaration

    Swift

    public private(set) var body: ErrorBody? { get }
  • Returns the Codable error body encoded into bytes in a given format (eg: JSON).

    This function should be used if the RequestError was created using init(_:body:), otherwise it will return nil.

    Note

    This function is primarily intended for use by the Kitura Router so that it can encode and send a custom error body returned from a codable route.

    Usage Example:

    do {
        if let errorBodyData = try error.encodeBody(.json) {
            ...
        }
    } catch {
        // Handle the failure to encode
    }
    

    Throws

    An EncodingError if the encoding fails.

    Throws

    An UnsupportedBodyFormatError if the provided BodyFormat is not supported.

    Declaration

    Swift

    public func encodeBody(_ format: BodyFormat) throws -> Data?

    Parameters

    format

    Describes the format that should be used (for example: BodyFormat.json).

    Return Value

    The Data object or nil if there is no body, or if the error was not initialized with init(_:body:).

  • Returns the Data error body as the requested Codable type.

    This function should be used if the RequestError was created using init(_:bodyData:format:), otherwise it will return nil.

    This function throws; you can use bodyAs(_:) instead if you want to ignore DecodingErrors.

    Note

    This function is primarily intended for use by users of KituraKit or similar client-side code that needs to convert a custom error response from Data to a Codable type.

    Usage Example:

    do {
        if let errorBody = try error.decodeBody(MyCodableType.self) {
            ...
        }
    } catch {
        // Handle failure to decode
    }
    

    Throws

    A DecodingError if decoding fails.

    Declaration

    Swift

    public func decodeBody<Body>(_ type: Body.Type) throws -> Body? where Body : Decodable, Body : Encodable

    Parameters

    type

    The type of the value to decode from the body data (for example: MyCodableType.self).

    Return Value

    The Codable object or nil if there is no body or if the error was not initialized with init(_:bodyData:format:).

  • Returns the Data error body as the requested Codable type.

    This function should be used if the RequestError was created using init(_:bodyData:format:), otherwise it will return nil.

    This function ignores DecodingErrors, and returns nil if decoding fails. If you want DecodingErrors to be thrown, use decodeBody(_:) instead.

    Note

    This function is primarily intended for use by users of KituraKit or similar client-side code that needs to convert a custom error response from Data to a Codable type.

    Usage Example:

    if let errorBody = error.bodyAs(MyCodableType.self) {
        ...
    }
    

    Declaration

    Swift

    public func bodyAs<Body>(_ type: Body.Type) -> Body? where Body : Decodable, Body : Encodable

    Parameters

    type

    The type of the value to decode from the body data (for example: MyCodableType.self).

    Return Value

    The Codable object or nil if there is no body, or if the error was not initialized with init(_:bodyData:format:), or if decoding fails.

Comparing RequestErrors

  • Returns a Boolean value indicating whether the value of the first argument is less than that of the second argument.

    Declaration

    Swift

    public static func < (lhs: RequestError, rhs: RequestError) -> Bool
  • Indicates whether two URLs are the same.

    Declaration

    Swift

    public static func == (lhs: RequestError, rhs: RequestError) -> Bool

Describing a RequestError

  • A textual description of the RequestError instance containing the error code and reason.

    Declaration

    Swift

    public var description: String { get }
  • The computed hash value for the RequestError instance.

    Declaration

    Swift

    public var hashValue: Int { get }
  • The HTTP status code for the error. This value should be a valid HTTP status code if inside the range 100 to 599, however, it may take a value outside that range when representing other types of error.

    Declaration

    Swift

    public var httpCode: Int { get }
  • Creates an error representing a HTTP status code.

    Declaration

    Swift

    public init(httpCode: Int)

    Parameters

    httpCode

    A standard HTTP status code.

Accessing constants representing HTTP status codes

  • HTTP code 100 - Continue

    Declaration

    Swift

    public static let `continue`: RequestError
  • HTTP code 101 - Switching Protocols

    Declaration

    Swift

    public static let switchingProtocols: RequestError
  • ok

    HTTP code 200 - OK

    Declaration

    Swift

    public static let ok: RequestError
  • HTTP code 201 - Created

    Declaration

    Swift

    public static let created: RequestError
  • HTTP code 202 - Accepted

    Declaration

    Swift

    public static let accepted: RequestError
  • HTTP code 203 - Non Authoritative Information

    Declaration

    Swift

    public static let nonAuthoritativeInformation: RequestError
  • HTTP code 204 - No Content

    Declaration

    Swift

    public static let noContent: RequestError
  • HTTP code 205 - Reset Content

    Declaration

    Swift

    public static let resetContent: RequestError
  • HTTP code 206 - Partial Content

    Declaration

    Swift

    public static let partialContent: RequestError
  • HTTP code 207 - Multi Status

    Declaration

    Swift

    public static let multiStatus: RequestError
  • HTTP code 208 - Already Reported

    Declaration

    Swift

    public static let alreadyReported: RequestError
  • HTTP code 226 - IM Used

    Declaration

    Swift

    public static let imUsed: RequestError
  • HTTP code 300 - Multiple Choices

    Declaration

    Swift

    public static let multipleChoices: RequestError
  • HTTP code 301 - Moved Permanently

    Declaration

    Swift

    public static let movedPermanently: RequestError
  • HTTP code 302 - Found

    Declaration

    Swift

    public static let found: RequestError
  • HTTP code 303 - See Other

    Declaration

    Swift

    public static let seeOther: RequestError
  • HTTP code 304 - Not Modified

    Declaration

    Swift

    public static let notModified: RequestError
  • HTTP code 305 - Use Proxy

    Declaration

    Swift

    public static let useProxy: RequestError
  • HTTP code 307 - Temporary Redirect

    Declaration

    Swift

    public static let temporaryRedirect: RequestError
  • HTTP code 308 - Permanent Redirect

    Declaration

    Swift

    public static let permanentRedirect: RequestError
  • HTTP code 400 - Bad Request

    Declaration

    Swift

    public static let badRequest: RequestError
  • HTTP code 401 - Unauthorized

    Declaration

    Swift

    public static let unauthorized: RequestError
  • HTTP code 402 - Payment Required

    Declaration

    Swift

    public static let paymentRequired: RequestError
  • HTTP code 403 - Forbidden

    Declaration

    Swift

    public static let forbidden: RequestError
  • HTTP code 404 - Not Found

    Declaration

    Swift

    public static let notFound: RequestError
  • HTTP code 405 - Method Not Allowed

    Declaration

    Swift

    public static let methodNotAllowed: RequestError
  • HTTP code 406 - Not Acceptable

    Declaration

    Swift

    public static let notAcceptable: RequestError
  • HTTP code 407 - Proxy Authentication Required

    Declaration

    Swift

    public static let proxyAuthenticationRequired: RequestError
  • HTTP code 408 - Request Timeout

    Declaration

    Swift

    public static let requestTimeout: RequestError
  • HTTP code 409 - Conflict

    Declaration

    Swift

    public static let conflict: RequestError
  • HTTP code 410 - Gone

    Declaration

    Swift

    public static let gone: RequestError
  • HTTP code 411 - Length Required

    Declaration

    Swift

    public static let lengthRequired: RequestError
  • HTTP code 412 - Precondition Failed

    Declaration

    Swift

    public static let preconditionFailed: RequestError
  • HTTP code 413 - Payload Too Large

    Declaration

    Swift

    public static let payloadTooLarge: RequestError
  • HTTP code 414 - URI Too Long

    Declaration

    Swift

    public static let uriTooLong: RequestError
  • HTTP code 415 - Unsupported Media Type

    Declaration

    Swift

    public static let unsupportedMediaType: RequestError
  • HTTP code 416 - Range Not Satisfiable

    Declaration

    Swift

    public static let rangeNotSatisfiable: RequestError
  • HTTP code 417 - Expectation Failed

    Declaration

    Swift

    public static let expectationFailed: RequestError
  • HTTP code 421 - Misdirected Request

    Declaration

    Swift

    public static let misdirectedRequest: RequestError
  • HTTP code 422 - Unprocessable Entity

    Declaration

    Swift

    public static let unprocessableEntity: RequestError
  • HTTP code 423 - Locked

    Declaration

    Swift

    public static let locked: RequestError
  • HTTP code 424 - Failed Dependency

    Declaration

    Swift

    public static let failedDependency: RequestError
  • HTTP code 426 - Upgrade Required

    Declaration

    Swift

    public static let upgradeRequired: RequestError
  • HTTP code 428 - Precondition Required

    Declaration

    Swift

    public static let preconditionRequired: RequestError
  • HTTP code 429 - Too Many Requests

    Declaration

    Swift

    public static let tooManyRequests: RequestError
  • HTTP code 431 - Request Header Fields Too Large

    Declaration

    Swift

    public static let requestHeaderFieldsTooLarge: RequestError
  • HTTP code 451 - Unavailable For Legal Reasons

    Declaration

    Swift

    public static let unavailableForLegalReasons: RequestError
  • HTTP code 500 - Internal Server Error

    Declaration

    Swift

    public static let internalServerError: RequestError
  • HTTP code 501 - Not Implemented

    Declaration

    Swift

    public static let notImplemented: RequestError
  • HTTP code 502 - Bad Gateway

    Declaration

    Swift

    public static let badGateway: RequestError
  • HTTP code 503 - Service Unavailable

    Declaration

    Swift

    public static let serviceUnavailable: RequestError
  • HTTP code 504 - Gateway Timeout

    Declaration

    Swift

    public static let gatewayTimeout: RequestError
  • HTTP code 505 - HTTP Version Not Supported

    Declaration

    Swift

    public static let httpVersionNotSupported: RequestError
  • HTTP code 506 - Variant Also Negotiates

    Declaration

    Swift

    public static let variantAlsoNegotiates: RequestError
  • HTTP code 507 - Insufficient Storage

    Declaration

    Swift

    public static let insufficientStorage: RequestError
  • HTTP code 508 - Loop Detected

    Declaration

    Swift

    public static let loopDetected: RequestError
  • HTTP code 510 - Not Extended

    Declaration

    Swift

    public static let notExtended: RequestError
  • HTTP code 511 - Network Authentication Required

    Declaration

    Swift

    public static let networkAuthenticationRequired: RequestError