Fork me on GitHub
#clojure-uk
<
2019-09-12
>
dominicm11:09:55

Curious: how do you model your domain layer? What does it return? How do you interpret that into http status codes?

mccraigmccraig12:09:37

@dominicm we generally have two paths...

mccraigmccraig12:09:48

in the first a domain fn returns a promise of a result type when successful, and an errored promise wrapping a [tag data] variant in the ex-info when it fails

mccraigmccraig12:09:40

in the second it returns a promise of [tag data] when successful too, and the same errored promise with [tag data] in the ex-info when it fails

mccraigmccraig12:09:43

tag gets mapped to an http status code either by default or by a map of {tag status} declared on the resource

mccraigmccraig12:09:29

(and by promise here i mean manifold/Deferred)

dominicm12:09:55

What's a tag?

mccraigmccraig12:09:38

they are namespaced keywords

mccraigmccraig12:09:22

this is from a couple of namespaces, so the aliases don't line up, but here's an example: https://gist.github.com/mccraigmccraig/7a27330dc57ec4bf37b9743cf6708d98

mccraigmccraig12:09:55

api-method-fn dispatches a multimethod with the resource key and passes the (merged) params from yada, then uses the tag-status-map (if provided, otherwise the default) to convert the result into an http response

mccraigmccraig12:09:51

the end result is that in the simplest case successful calls get a 200 and any errors a 500, but should finer control be appropriate then the domain layer returns something that makes sense in its world, which then gets mapped to an http status

dominicm12:09:37

Are they essentially all names for http, or do they differ in some way?

mccraigmccraig12:09:55

are what all names for http ?

mccraigmccraig12:09:09

the domain-layer response tags ?

dominicm12:09:49

Are the domain tags all e.g. Unauthorized which maps directly to http, or do you have increased/decreased granularity?

mccraigmccraig12:09:26

entirely up to a particular resource implementation. some resources return partial success type tags which all get an http 200, some resources have various failures which all map to a 500. generally the domain layer returns something that makes sense for it regardless of the transport

mccraigmccraig12:09:05

and in the unanticipated failure case (i.e. an errored promise carrying an exception payload), the exception gets mapped to a generic failure tag and result in a 500, unless overridden

dominicm12:09:20

ah, so you do have more nuance than http in some cases. That's what I'd figured would happen.

mccraigmccraig12:09:41

yes - it's relatively rare though. most resources just use the default success=200/failure=500

mccraigmccraig12:09:03

(crosscutting stuff like auth is done in an interceptor and precedes this flow)

dominicm13:09:51

Yeah, it's an interesting decision where to do auth. Http seems to want you to do it quite early. So domain layers tend to assume authenticated, and sometimes authorized.

mccraigmccraig15:09:41

yep: we assume authenticated, and have a walker which applies an authentication interceptor to authenticated resources. authorization hasn't found a declarative form yet and is generally done ad-hoc inside the domain layer

dominicm15:09:07

We started to investigate declarative authorization. There's a big world out there.