Help with handling http error status

I’m trying to follow the example at One-of variants — Tapir 1.x documentation but I’m getting

IllegalArgumentException Cannot decode: Mismatch(500,404)

instead of the expected Left[ErrorInfo,Unit] . My endpoint looks like

ndpont

.get

.in("_ready")

.in(query[List[String]]("checked"))

.out(statusCode(StatusCode.Ok))

.errorOut(

oneOf[ErrorInfo](

oneOfVariant(statusCode(StatusCode.InternalServerError).and(jsonBody[ReadyCheckFailed])), //Failed a ready check oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound])), //404 when there's no /_ready oneOfVariant(statusCode(StatusCode.Unauthorized).and(jsonBody[Unauthorized])), //sometimes a 401 when there's no /_ready oneOfVariant(statusCode(StatusCode.InternalServerError).and(jsonBody[InternalServerError])), //Some other 500

)

)

with
sealed trait ErrorInfo case class ReadyCheckFailed(failedChecks:List[String]) extends ErrorInfo
case class NotFound(what: String) extends ErrorInfo

case class Unauthorized(realm: String) extends ErrorInfo

case class InternalServerError(msg: String) extends ErrorInfo

What did I miss?

Thanks,

David

I figured it out. For general errors json bodies aren’t going to be there. I used stringBody.mapTo[NotFound] and the like instead.

So this was when using the endpoint as a client, and receiving a 500 response with a non-JSON body? Maybe there is something we could do to make the errors better (or richer).

1 Like

Thanks!

The scala ecosystem is maybe settling on throwing Exceptions - because so many things can go wrong - and returning Either[BrokenThingsYouThoughtAbout, WhatYouWant] s . Bill Venners predicted that at NEScala in 2015 “The Good, The Bad, and The Ugly” .

It looks like infallibleEndpoint will give me the ugly Exceptions I want. That might be a better starting point for URLs in many cases. I sure didn’t find it on my own.

endpoint is going to hide the Ugly. That probably can’t change until Tapir 2.x; it breaking existing code. I don’t think you should touch it.

Maybe add a paragraph about infallibleEndpoint at the start of error handling in the docs?

Ah didn’t know about Bill’s talk, it seems he was right :slight_smile:

As for the docs, do you mean this page? Error handling — Tapir 1.x documentation

That’s the page. Thanks again!