Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MultipartParserError Public #112

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions Sources/MultipartKit/MultipartParserError.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
/// Technical parsing error, such as malformed data or invalid characters.
/// This is mainly used by ``MultipartParser``.
package enum MultipartParserError: Swift.Error, Equatable {
case invalidBoundary
case invalidHeader(reason: String)
case invalidBody(reason: String)
public struct MultipartParserError: Swift.Error, Equatable, Sendable {
public struct ErrorType: Equatable, CustomStringConvertible {
enum Base: String, Equatable {
case invalidBoundary
case invalidHeader
case invalidBody
}

let base: Base

private init(_ base: Base) {
self.base = base
}

public static let invalidBoundary = Self(.invalidBoundary)
public static let invalidHeader = Self(.invalidHeader)
public static let invalidBody = Self(.invalidBody)

public var description: String {
base.rawValue
}
}

private struct Backing: Equatable, Sendable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you need the Backing type. What is this giving us over just adding the following to MultipartParserError

let errorType: ErrorType
let reason: String?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the Backing type because it provides a single place to modify error-related data without exposing too much detail in MultipartParserError.
And If we later want to add more fields (e.g., identifier, underlyingError, or HTTPStatusCode), we can do so without cluttering the main struct.

@adam-fowler will you in the favour of remove Backing type.

let errorType: ErrorType
let reason: String?
}

private var backing: Backing

public var errorType: ErrorType { backing.errorType }
public var reason: String? { backing.reason }

private init(backing: Backing) {
self.backing = backing
}

private init(errorType: ErrorType) {
self.backing = .init(errorType: errorType, reason: nil)
}

public static let invalidBoundary = Self(errorType: .invalidBoundary)

public static func invalidHeader(reason: String) -> Self {
.init(backing: .init(errorType: .invalidHeader, reason: reason))
}

public static func invalidBody(reason: String) -> Self {
.init(backing: .init(errorType: .invalidBody, reason: reason))
}

public var description: String {
if let reason = reason {
return "MultipartParserError(errorType: \(errorType), reason: \(reason))"
} else {
return "MultipartParserError(errorType: \(errorType))"
}
}
}