Comments (4)
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.)
from siesta.
Mathew — I’m closing this one, but please feel free to keep following up (or reopen if it’s not resolve for you).
from siesta.
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.
from siesta.
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.
from siesta.
Related Issues (20)
- Possible memory leak in ProgressTracker HOT 13
- tvOS Carthage compilation broken for Xcode 9 HOT 2
- Swift 4.2 + Xcode 10 HOT 5
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:66: 'UIApplicationDidReceiveMemoryWarning' has been renamed to 'UIApplication.didReceiveMemoryWarningNotification' HOT 1
- Pods/Siesta/Source/Siesta/Support/WeakCache.swift:14:42: Type 'NSNotification.Name' has no member 'UIApplication' HOT 1
- Readme 404 link HOT 2
- unexpected duplicate task error for new Xcode build system from Xcode 10 IOS 12.0.1 HOT 1
- How to refresh same web service ?
- Wildcard in PipelineStage.add(_, contentTypes:) doesn't work as expected
- RemoteImageView placeholderImage not showing when imageResource or ImageURL is nil HOT 2
- File-based caching for Siesta HOT 4
- Unexpected version number in 'available' attribute for non-specific platform '*' HOT 2
- Cached Resource timestamp refresh after loading
- Proposal: More elegant way to avoid memory leak in closures HOT 4
- Y'all got wiki-spammed HOT 2
- Chained request progress support
- 404 errors from a resource don't clear as expected from a resource("static url").load()
- Xcode SPM will not accept Quick@around-each dependency HOT 7
- The package product 'Alamofire' requires minimum platform version 10.0 for the tvOS platform, but this target supports 9.0 HOT 3
- Quick dependancy is not correctly versioned HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from siesta.