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

enhancement - example stubbing API errors #214

Closed
johndpope-karhoo opened this issue Oct 5, 2016 · 5 comments
Closed

enhancement - example stubbing API errors #214

johndpope-karhoo opened this issue Oct 5, 2016 · 5 comments

Comments

@johndpope-karhoo
Copy link

Probably the most valuable part of stubbing is being able to recreate server side errors.
Here's a snippet of some -


struct APIError {
    private static let domain = "com.yourapp.app"
    static let BadRequest = NSError(domain: domain, code: 400, userInfo: ["NSLocalizedDescription":"Bad Request"])
    static let Unauthorized = NSError(domain: domain, code: 401, userInfo: ["NSLocalizedDescription":"Unauthorized Request"])
    static let NotFound = NSError(domain: domain, code: 404, userInfo: ["NSLocalizedDescription":"Data Not Found"])
    static let RateLimitExceeded = NSError(domain: domain, code: 429, userInfo: ["NSLocalizedDescription":"Rate Limit Exceeded"])
    static let NoResponse = NSError(domain: domain, code: 444, userInfo: ["NSLocalizedDescription":"No Response"])
    static let InternalServerError = NSError(domain: domain, code: 500, userInfo: ["NSLocalizedDescription":"Internal Server Error"])
    static let ServiceUnavailable = NSError(domain: domain, code: 503, userInfo: ["NSLocalizedDescription":"Service Unavailable"])
    static let Unknown = NSError(domain: domain, code: 520, userInfo: ["NSLocalizedDescription":"Unknown Error"])
    static let ErrorGettingTrip = NSError(domain: domain, code: 62, userInfo: ["NSLocalizedDescription":"Error getting something specific"])

    static func FromCode(code: Int) -> NSError {
        switch(code) {
        case 400:
            return BadRequest
        case 401:
            return Unauthorized
        case 404:
            return NotFound
        case 429:
            return RateLimitExceeded
        case 444:
            return NoResponse
        case 500:
            return InternalServerError
        case 503:
            return ServiceUnavailable
        default:
            return Unknown
        }
    }
}

/// Sprinkle Chaos mode

let random = Int(arc4random_uniform(20))
                    if (random == 1) {
                        let notConnectedError = NSError(domain:NSURLErrorDomain, code:Int(CFNetworkErrors.CFURLErrorNotConnectedToInternet.rawValue), userInfo:nil)
                        return OHHTTPStubsResponse(error:notConnectedError)
                    }
                    if (random == 2) {
                        let timeOutError = NSError(domain:NSURLErrorDomain, code:Int(CFNetworkErrors.CFURLErrorTimedOut.rawValue), userInfo:nil)
                        return OHHTTPStubsResponse(error:timeOutError)
                    }

                    if (random == 3) {
                        let timeOutError = NSError(domain:NSURLErrorDomain, code:Int(HTTP_CODE_TIMEOUT.rawValue), userInfo:nil)
                        return OHHTTPStubsResponse(error:timeOutError)
                    }

@AliSoftware
Copy link
Owner

Hi

I'm not sure what's your point with that issue?
Are you suggesting to add that example to the other usage examples already in the dedicated wiki page to have a more concrete example?

@johndpope-karhoo
Copy link
Author

I love this project - thanks for the time you spent on.
I am finding more uses for it each day.

It maybe worthwhile sending users to pre-canned search on github gist
https://gist.github.com/search?utf8=%E2%9C%93&q=OHHTTPStubs

This serves a couple of purposes -

  1. other users sharing their OHHTTPStubs gists
  2. curating better scripts.

I didn't find a single script to encapsulate my needs - but mocking server side errors has now become biggest use case that every app should have - and I want a canonical example that can handle every edge case. Happy to contribute once I have more time.

@AliSoftware
Copy link
Owner

AliSoftware commented Oct 5, 2016

Thx for the feedback!

You should be able to contribute to the wiki already (contributions, modifications and creating new pages in the wiki are open, no need for special rights or pull requests), so don't hesitate to enrich the existing Usage Examples page… or maybe even create a dedicated new wiki page for sharing such more complete code snippets like this!

@wwe-johndpope
Copy link

wwe-johndpope commented Mar 17, 2017

I wanted to share this swift 3 gist - I looked at the sample wiki but not sure how to edit it - feel free to rip off the content and drop it in. Maybe you need a swift sample page entirely.
This has local swagger json compatibility
https://gist.github.com/wwe-johndpope/60ac94a1af4537d755c71119d61f94f8

@AliSoftware
Copy link
Owner

I've shared all your suggestions in this wiki page now, thanks for the suggestions 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants