-
Notifications
You must be signed in to change notification settings - Fork 13
RESPDecodeError: Version 2 #233
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
Merged
+238
−62
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a0d8e2
Add RESPDecodeError, somehow broken arrays though
adam-fowler 0a4f85a
Restructure RESPDecodeError to code, message and token
adam-fowler b3e2422
Add tests for decode errors
adam-fowler ed55165
Merge branch 'main' into decode-error
adam-fowler bbc4a08
Add expected size to RESPDecodeError.invalidArraySize
adam-fowler 36b3554
Fix invalid license header in RESPDecodeErrorTests.swift
adam-fowler 4f473fd
Documentation fix
adam-fowler 3046226
Merge branch 'main' into decode-error
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// This source file is part of the valkey-swift project | ||
// Copyright (c) 2025 the valkey-swift project authors | ||
// | ||
// See LICENSE.txt for license information | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
/// Error returned when decoding a RESPToken. | ||
/// Error thrown when decoding RESPTokens | ||
public struct RESPDecodeError: Error { | ||
/// Error code for decode error | ||
public struct ErrorCode: Sendable, Equatable, CustomStringConvertible { | ||
fileprivate enum Code: Sendable, Equatable { | ||
case tokenMismatch | ||
case invalidArraySize | ||
case missingToken | ||
case cannotParseInteger | ||
case cannotParseDouble | ||
case unexpectedToken | ||
} | ||
|
||
fileprivate let code: Code | ||
fileprivate init(_ code: Code) { | ||
self.code = code | ||
} | ||
|
||
public var description: String { String(describing: self.code) } | ||
|
||
/// Token does not match one of the expected tokens | ||
public static var tokenMismatch: Self { .init(.tokenMismatch) } | ||
/// Does not match the expected array size | ||
public static var invalidArraySize: Self { .init(.invalidArraySize) } | ||
/// Token is missing | ||
public static var missingToken: Self { .init(.missingToken) } | ||
/// Failed to parse an integer | ||
public static var cannotParseInteger: Self { .init(.cannotParseInteger) } | ||
/// Failed to parse a double | ||
public static var cannotParseDouble: Self { .init(.cannotParseDouble) } | ||
/// Token is not as expected | ||
public static var unexpectedToken: Self { .init(.unexpectedToken) } | ||
} | ||
public let errorCode: ErrorCode | ||
public let message: String? | ||
public let token: RESPToken.Value | ||
|
||
public init(_ errorCode: ErrorCode, token: RESPToken.Value, message: String? = nil) { | ||
self.errorCode = errorCode | ||
self.token = token | ||
self.message = message | ||
} | ||
|
||
public init(_ errorCode: ErrorCode, token: RESPToken, message: String? = nil) { | ||
self = .init(errorCode, token: token.value, message: message) | ||
} | ||
|
||
/// Token does not match one of the expected tokens | ||
public static func tokenMismatch(expected: [RESPTypeIdentifier], token: RESPToken) -> Self { | ||
if expected.count == 0 { | ||
return .init(.tokenMismatch, token: token, message: "Found unexpected token while decoding") | ||
} else if expected.count == 1 { | ||
return .init(.tokenMismatch, token: token, message: "Expected to find a \(expected[0])") | ||
} else { | ||
let expectedTokens = "\(expected.dropLast().map { "\($0)" }.joined(separator: ", ")) or \(expected.last!)" | ||
return .init(.tokenMismatch, token: token, message: "Expected to find a \(expectedTokens) token") | ||
} | ||
} | ||
/// Does not match the expected array size | ||
public static func invalidArraySize(_ array: RESPToken.Array) -> Self { | ||
.init(.invalidArraySize, token: .array(array)) | ||
} | ||
/// Token associated with key is missing | ||
public static func missingToken(key: String, token: RESPToken) -> Self { | ||
.init(.missingToken, token: token, message: "Expected map to contain token with key \"\(key)\"") | ||
} | ||
} | ||
|
||
extension RESPDecodeError: CustomStringConvertible { | ||
public var description: String { | ||
"Error: \"\(self.message ?? String(describing: self.errorCode))\", token: \(self.token.debugDescription)" | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could be worth adding a message just like
tokenMismatch()
to make this error more descriptive. For example for empty array, the message can say "Empty Array" and so on.