Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tapiro can't return non-200 status codes, except for 4xx #183

Closed
giuscri opened this issue Mar 5, 2020 · 4 comments
Closed

tapiro can't return non-200 status codes, except for 4xx #183

giuscri opened this issue Mar 5, 2020 · 4 comments

Comments

@giuscri
Copy link
Contributor

giuscri commented Mar 5, 2020

tapiro should at least provide the possibility of returning 5xx status codes

/cc @calippo

@calippo
Copy link
Member

calippo commented Mar 10, 2020

This can be done using statusCodes fn as used here

Term.Apply(Term.Name("statusCodes"), List(Lit.String(member.name))),

Here you have an example:

object ControllerEndpoints {

  def create[AuthToken](
      statusCodes: String => StatusCode = _ => StatusCode.UnprocessableEntity
  )(
      implicit codec0: JsonCodec[SpittyCash],
      codec1: JsonCodec[Errore.Errorone],
      codec2: JsonCodec[Errore.Errorino],
      codec3: PlainCodec[String]
  ) = new ControllerEndpoints[AuthToken] {
    override val ghetto: Endpoint[String, Errore, SpittyCash, Nothing] =
      endpoint.get
        .in("ghetto")
        .in(query[String]("at"))
        .errorOut(
          oneOf[Errore](
            statusMapping(statusCodes("Errorone"), jsonBody[Errore.Errorone]),
            statusMapping(statusCodes("Errorino"), jsonBody[Errore.Errorino])
          )
        )
        .out(jsonBody[SpittyCash])
  }
}

Improving the way this is done is part of #68

@giuscri
Copy link
Contributor Author

giuscri commented Mar 10, 2020

This means I have to create error types like CreateCampingError, is that correct?

TaggedUnion(
name = "CreateCampingError",
values = List(
TaggedUnion.Member(
name = "DuplicateName",
params = List(
CaseClass.Member(
name = "names",
tpe = Type.Name("SuggestedNames"),
desc = Some("suggestions for names that are not in use"),
),
),
desc = Some("The name is already in use"),
),
TaggedUnion.Member(
name = "SizeOutOfBounds",
params = List(
CaseClass.Member(name = "min", tpe = Type.Name("Int"), desc = None),
CaseClass.Member(name = "max", tpe = Type.Name("Int"), desc = None),
),
desc = Some("The chosen size is not allowed"),
),
TaggedUnion.Member(
name = "OtherError",
params = List(),
desc = None,
),
),
desc = Some("Errors that can happen when creating a camping"),
`package` = List("io", "buildo", "baseexample", "models"),
),

@calippo
Copy link
Member

calippo commented Mar 10, 2020

Correct, in the previous example the controller would be

sealed trait Errore
object Errore {
  case class Errorone(message: String) extends Errore
  case class Errorino(message: String) extends Errore
}

trait Controller[F[_], AuthToken] {
  @query
  def ghetto(at: String): F[Either[Errore, ...]]
}

@calippo
Copy link
Member

calippo commented Mar 10, 2020

@giuscri waiting your ack to close this, already linked in #68

@calippo calippo closed this as completed Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants