bustoutsolutions / siesta

The civilized way to write REST API clients for iOS / macOS
https://bustoutsolutions.github.io/siesta/
MIT License
2.19k stars 159 forks source link

Cannot create Siesta.Error.InvalidJSONObject #37

Closed MPiccinato closed 8 years ago

MPiccinato commented 8 years ago

I am attempting throw an error on all "text/*" responses.

I copied the 'TextResponseTransformer' func into my own class in attempt to reduce it to...

public func TextResponseTransformer(transformErrors: Bool = true) -> ResponseTransformer
{
    return ResponseContentTransformer(transformErrors: transformErrors)
        {
            (content: NSData, entity: Entity) throws -> String in
            let error = Siesta.Error(userMessage: "Response transformation failed.", cause: Error.Cause.InvalidJSONObject())
    }
}

Xcode7.2 is giving me this error 'Cannot invoke value of type 'Error.Cause.InvalidJSONObject.Type' with argument list '()'

pcantrell commented 8 years ago

The error you’re getting is because InvalidJSONObject doesn’t have a public initializer. (It’s only meant for client code to detect, not to create.) To do it using the approach you were using, you can make up your own error type:

enum MyAppErrors: ErrorType {
    case ResponsesMustBeJSON
}

…and then:

service.configure {
    $0.config.responseTransformers.add(
        ResponseContentTransformer() {
            (content: Any, entity: Entity) throws -> Any in
            guard content is NSJSONConvertible else {
                throw Siesta.Error(
                    userMessage: "Cannot parse response.",
                    cause: MyAppErrors.ResponsesMustBeJSON)
            }
            return content
        }
    )
}

But wait! Siesta already has that type checking built in. Siesta infers a content transformer’s expected input type from the parameter type of the closure you give, and flags it as an error if the incoming type in the pipeline is wrong. So believe it or not, all you have to do is pass a closure that declares a specific type for the content, but just passes it through unmodified:

service.configure {
    $0.config.responseTransformers.add(
        ResponseContentTransformer() {
            (content: NSJSONConvertible, entity: Entity) in   // Note: NSJSONConvertible instead of Any
            content
        }
    )
}

And because adding a custom ResponseContentTransformer is so common, Siesta provides a shortcut:

service.configureTransformer("/**") {
    (content: NSJSONConvertible, entity: Entity) in
    content
}

…or if you’re willing to lean hard on some Swift type inference magic:

service.configureTransformer("/**") {
    $0.content as NSJSONConvertible
}

That’s all you need!


Note that all the code above lets the default content-type-based transformers do their thing, then errors if something other than a JSON-like array or dict came out the other end.

If instead you want to attempt to parse everything as JSON, regardless of content type:

configure {
    $0.config.responseTransformers.add(JSONResponseTransformer())
}

By default, Siesta applies JSON parsing only to responses with a content type of */json or */*+json. This additional config makes Siesta apply JSON parsing to everything.

You want might want to parse even plain text responses as JSON. To do that, pass useDefaultTransformers: false to your Service initializer. (Why? Any transformers you add get appended to ones that are already there. Siesta includes transformers for text, images, and JSON by default. With those default ones in there, the server could return valid JSON with a content type of text/plain, the default text transformer would catch that and turn that into a string, and then the JSON transformer would say, “Hey! I’m supposed to get NSData” and give you an error.)

pcantrell commented 8 years ago

Mathew — I’m closing this one, but please feel free to keep following up (or reopen if it’s not resolve for you).

MPiccinato commented 8 years ago

Thanks for the detailed response! That last part was exactly what I was looking for.

I assumed that the Error structs were useable since they are "public" structs. I guess I need to go back and read up on that.

pcantrell commented 8 years ago

The Error.Cause structs are not publicly constructable because synthesized struct initializers aren't public (I think).

The causes themselves are public (even though their initializers aren't) so that you can do error-specifc recovery. There’s an example of that in the docs.