Fork me on GitHub
#yada
<
2016-10-07
>
jsyrjala06:10:17

How to make an exception handler that will allow me to make a custom error page? Specifically I'd like to generate json document in case of any error.

malcolmsparks07:10:19

@jsyrjala search the doc for 'status responses', that's the feature you need

stijn09:10:43

is it by design that header values cannot be coerced? the use case is e.g. an X-Auth-Token that should become a UUID.

stijn09:10:02

declaring the resource works, but when performing a request to it, it returns a 400 {:error {\"x-auth-token\" (not (instance? java.util.UUID a-java.lang.String))}}

mishok1312:10:23

I've just submitted a bug report https://github.com/juxt/yada/issues/126 but I'm also available here for next couple of hours to help with reproducing the issue

malcolmsparks12:10:54

@stijn no, not by design. Would be a nice feature

malcolmsparks12:10:32

@mishok13 thanks, was actuwlly going to ask you did that, yesterday was #xt16 and I was very busy!

mishok1312:10:09

I'm quite perplexed as to what's going on there

mishok1312:10:33

seems that exception is raised initially by yada.representation/select-best-representation due to config mismatch but then yada.handler/handle-request-with-maybe-subresources calls that same function again leading to missing proper exception info

malcolmsparks12:10:18

@mishok13 background context is that when there is an error, yada renegotiates the charset. Say your client can parse UTF-8 and Shift_JIS, but your resource can only produce Shift_JIS, but there's any error - do you want that error to be returned in Shift_JIS or UTF-8? I assume you'd want it in UTF-8, especially if there's an English error message. That's why yada renegotiates. Because the error, in this case, is actually a yada internal error, it's going a bit crazy. I think the best thing for me to do is to fix the original error in this case. We still have this issue where yada should distinguish betweeen application and its own internal errors, but that's something I'm mulling over how to do

malcolmsparks12:10:49

I'm going to attempt a fix now and release an update

lmergen12:10:06

one solution (or hack, depending on your point of view) could be to optionally pre-set a charset for error responses

lmergen12:10:44

having said that, i don’t think there’s any webserver out there that actually negotiates charset for error pages 🙂

mishok1313:10:02

I'd argue that being somewhat more lenient is a better route, especially given that the spec clearly allows that: > If an Accept-Charset header is present, and if the server cannot send a response which is acceptable according to the Accept-Charset header, then the server SHOULD send an error response with the 406 (not acceptable) status code, though the sending of an unacceptable response is also allowed.

malcolmsparks13:10:38

the bug is an oversight on my part, I'll definitely fix it

bhagany20:10:03

I’m upgrading from 1.1.31 -> 1.1.37, and I’m running into a small issue with defining my own mime types. I’d like to provide a method for process-request-body for my mime type, but I don’t need any special functionality. Other process-request-body methods just call default-process-request-body, but this function is private. I can obviously work around this, but is there a reason for it to be private that I’m not seeing?

bhagany20:10:04

heh, my copy and paste workaround isn’t going well either, because default-process-request-body calls other fns that are also private

bhagany20:10:21

(again, workaroundable, but still)

bhagany20:10:23

alright. would you like another 1-char pull request from me?

malcolmsparks20:10:27

no, I've done it -try 1.1.38

bhagany20:10:34

wow, fast! thanks!

malcolmsparks20:10:02

but if you spot any more, just fix and lein install, then PR me

bhagany20:10:56

will do, thank you

bhagany20:10:32

everything’s working fine for me under 1.1.38

ericnormand22:10:17

I'm learning yada and I have a question

ericnormand22:10:32

when using yada with bidi, do we lose the bidirectional nature of bidi?

ericnormand22:10:39

how do you generate urls?

ericnormand22:10:23

do you pass it the entire resource?

malcolmsparks23:10:03

Hi @ericnormand - welcome to the channel!

malcolmsparks23:10:07

In yada, you can use bidi just like normal. You can place yada resources in the same place you'd put Ring handlers, and use bidi's tag function to tag them. You can then use bidi's path-for function to create URLs to those resources, using the tag as the first parameter

malcolmsparks23:10:43

But it doesn't stop there...

malcolmsparks23:10:02

yada resources can have an :id entry, usually with a keyword value. That can be used in bidi's path-for function too.

malcolmsparks23:10:38

bidi has a vhosts feature now, whereby you specify the scheme/host/port as well as the routes - this allows bidi to create full URIs, which are often useful when doing complete HTTP because some parts of the spec. ask for absolute URLs (Location headers, for instance).

malcolmsparks23:10:38

If you use vhosts (if you don't' know the name of the host you'll be deploying to you can use a wildcard), then yada provides convenient access via its functions in yada.context, which are also accessible via yada.yada - it's recommended to require yada.yada to bring in yada's 'public' parts.

malcolmsparks23:10:12

One such function is url-for, to get the full URL of the resource you want to target.

malcolmsparks23:10:21

So, for example, suppose you had the following resource:

malcolmsparks23:10:04

(yada.yada/resource {:id :foo :methods {:get {:response (fn [_] "hello")}}})

malcolmsparks23:10:29

You can form a URI to it with (yada.yada/url-for ctx :foo)

malcolmsparks23:10:31

ctx is yada's request context and its' passed to every callback so you'll always have access to it

malcolmsparks23:10:59

Since ctx contains the request information, it's also possible to calculate the relative path to a resource, with yada.yada/href-for

malcolmsparks23:10:37

And there's yada.yada/host-for, path-for, and scheme-for...

malcolmsparks23:10:03

Whether you want an absolute URI or relative one depends on what you're doing. If you're generating hypermedia links in JSON request bodies, I'd recommend you use absolute URLs (`url-for`), but if you're generating an HTML form, href-for will be fine.

malcolmsparks23:10:00

Finally, all these methods take options, so you can specify :route-params and query-params, which are often needed to generate the full URL.

malcolmsparks23:10:36

So in answer to your question, you pass it the :id of the resource, not the entire resource.

malcolmsparks23:10:19

If you want to target a particular resource, give it an id - and I recommend you use a (namespaced) keyword for the value.

malcolmsparks23:10:14

In my projects I tend to use a single well-known namespace (rather than the ::keyword syntax), so that link-generation survives any refactoring I might do