-
-
Notifications
You must be signed in to change notification settings - Fork 44
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, however this won't roll as it is. If we make the enum public as it is it will be set in stone and we won't be able to update it without breaking the API. Could you follow this approach instead? https://github.com/vapor/jwt-kit/blob/main/Sources/JWTKit/JWTError.swift
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @nakul-py this is much better. We need some small changes though.
Co-authored-by: Mahdi Bahrami <[email protected]>
Co-authored-by: Mahdi Bahrami <[email protected]>
…ltipart-kit into multipartparsererror
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final changes hopefully, from my side at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me other than these 2 spaces. @ptoffy please take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits but looking good
Also @nakul-py this should go through a |
With the error constructed like this there is no way to extract the reason associated values. |
There is still an issue here in that I cannot easily catch these errors without know the reason string eg do {
try multipartFormThings()
} catch let error as MultipartParserError where error == .invalidHeader("I need to know what string this is") {
...
} @ptoffy Are the associated strings in the error necessary? If they are can we do something like this https://github.com/apple/swift-nio/blob/main/Sources/NIOFileSystem/FileSystemError.swift where the code and reason are separate. |
I think as I said before we should follow JWTKit's example and add something like https://github.com/vapor/jwt-kit/blob/main/Sources/JWTKit/JWTError.swift#L90 so you can do catch let error as MultipartParserError where error.errorType == .invalidHeader and you then have access to the |
To be honest I'd prefer to just have an enum with a new case |
How does adding a DO_NOT_USE_OR_ELSE case help? |
} | ||
} | ||
|
||
private struct Backing: Equatable, Sendable { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Well if there is such a case, it solves the exhaustive switch problem. Then we can just mark the enum as public. |
I'd vote not to use this - it's not a particularly nice solution and means that any new cases get blackholed into that catch all that just causes confusion because now people have to switch over the |
It does have downsides, but the upside is it's much easier to maintain and you can use associated values with each case in a much nicer way. Also get the pattern matching stuff like in catch blocks. I would go this route personally, but I don't think y'all like this (I mean you mentioned it too) so I'm just trying to explain the approach. |
Summary
This PR makes the MultipartParserError publicly accessible within the MultipartKit package.
Fixes #111