Skip to content

Matching errors without checking NSError's userInfo dictionary #615

Closed

Description

@csiszarattila

Hi,

I try to write a simple match for an UrlError:

  expect(error).to(
     matchError(NSError(domain: NSURLErrorDomain, code: URLError.timedOut.rawValue, userInfo: nil)
  )

But it fails, even the error is the same, because matchError seems to also compare the userInfo:

expected to match error <Error Domain=NSURLErrorDomain Code=-1001 "(null)">, got <Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x600001431260 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://example.org/example/end/point, NSErrorFailingURLKey=https://example.org/example/end/point, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}>

Is it intentional? Is there a way to overcome this?

Thanks.

Activity

  1. ikesyo commented on Jan 20, 2019

    @ikesyo
    Member

    I'm sorry for the late response.

    I think that's because NSError equality check is implemented as such and the overload is used:

    public func matchError<T: Error & Equatable>(_ error: T) -> Predicate<Error> {
    return Predicate.define { actualExpression in
    let actualError = try actualExpression.evaluate()
    let failureMessage = FailureMessage()
    setFailureMessageForError(
    failureMessage,
    postfixMessageVerb: "match",
    actualError: actualError,
    error: error
    )
    var matches = false
    if let actualError = actualError as? T, error == actualError {
    matches = true
    }
    return PredicateResult(bool: matches, message: failureMessage.toExpectationMessage())
    }
    }

    So I'd say this is an expected behavior.

    But it may be good to provide another overload which takes an NSError and a matching strategy such as:

    enum NSErrorMatchingStrategy {
        case `default` // Rely on `NSError`'s `Equatable` implementation
        case ignoringUserInfo // Just compare `domain` and `code` and ignore `userInfo`
    }
    
    func matchError(_ error: NSError, strategy: NSErrorMatchingStrategy = .default) -> Predicate<NSError>
  2. locked and limited conversation to collaborators on May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions