Fork me on GitHub
#off-topic
<
2016-12-29
>
timgilbert16:12:23

@roelof: it’s a little hard to say without knowing more about how you’ve set up your code, but in general if your flow if user requests page from you -> you make a call to the JSON API -> API result fails spec, I’d say the easiest thing to do is just render an error page instead of a “display the painting” page

roelof16:12:30

oke, and can I do the render from the code or only from a routes function ?

timgilbert16:12:45

Uh, I guess I’d do it from the code

timgilbert16:12:50

Like, somewhere you have code that fires off an HTTP request, and then code that renders the page when the HTTP result comes back

timgilbert16:12:17

I’d probably check the result when it comes back and then choose whether to render an error page or a normal page

timgilbert16:12:38

All of that stuff could be pretty agnostic to routing, but again it’s hard to say without knowing more about your application

roelof16:12:39

im still working on the input validation with spec

timgilbert17:12:23

I see… I think my approach would probably be here, where you get the result of (client/get url options): https://github.com/rwobben/paintings/blob/master/src/clj/paintings2/routes/home.clj#L26-L33

timgilbert17:12:48

…I’d probably bind the result of (client/get) to a local variable in that function, then run your (s/validate) to see if it matches your spec, and then if it doesn’t call like (layout/render “incorrect-data-found.html”) or something

timgilbert17:12:42

You could probably generalize that out into a function eventually so you could reuse it for the detail page, maybe you’d have something like this:

(defn check-for-errors [client-get-result spec-name function-to-call-on-success]
  (if (s/valid? client-get-result spec-name)
    (function-to-call-on-success client-get-result)
    (layout/render “error-in-json.html” …)))

(defn home-page [page]
  (let [result (client/get …)]
    (check-for-errors result ::page (fn [result] (-> result api/read-numbers
                                                                        api/fetch-paintings-and-images-front-page))))

timgilbert17:12:58

I’m sure there are a lot of other ways you could set it up, too. Anyways, for this stuff I don’t think you would need to mess with the routing, which just controls the association of your code and a URL.