# Custom error responses

**URL:** <https://softwaremill.community/t/custom-error-responses/358>\
**Category:** General\
**Created:** [January 10, 2024, 1:00pm UTC](https://softwaremill.community/t/custom-error-responses/358 "2024-01-10T13:00:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elmer](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@Elmer](https://softwaremill.community/u/Elmer)\
**Post date:** [January 10, 2024, 1:00pm UTC](https://softwaremill.community/t/custom-error-responses/358/1 "2024-01-10T13:00:56Z")

</div>

## Hi, I’m struggling to figure out how to send back an error message when something goes wrong during a request. I have the following endpoint:

case class ErrorResponse(message: String)  
case class CreatedDid(did: String)  
val createKeyDid: PublicEndpoint[Unit, Unit, CreatedDid, Any] = endpoint.get  
.in(“did” / “create-key”)  
.out(  
jsonBody[CreatedDid]  
)

val createKeyDidServerEndpoint: ServerEndpoint[Any, IO] =  
createKeyDid.serverLogicSuccess(\_ =\>

```
  val requestBody = KeyCreateDidRequestNew(method = "key")
  val url = Uri.parse(AppConfig.ssiKitCoreUrl ++ "/v1/did/create").getOrElse(uri"http://127.0.0.1:7000/v1/did/create")
  val response = client.send(basicRequest.post(url).body(requestBody).contentType("application/json"))

  if (response.code == StatusCode.Created) {
    println(s"Created did:key: ${response.body}")
    IO.pure(CreatedDid(did = response.body.getOrElse("Problem reading did from response body")))
  } else {
    throw new Exception("Problem creating key did")
  }
)

```

* * *

Instead of an exception like I’m throwing there, I would like to send back a ErrorResponse with a message.

What is the best way to do this?

---

<div class="post-metadata">

**Author:** ![adamw](https://dub1.discourse-cdn.com/flex005/user_avatar/softwaremill.community/adamw/32/28_2.png) [@adamw](https://softwaremill.community/u/adamw)\
**Post date:** [January 10, 2024, 1:28pm UTC](https://softwaremill.community/t/custom-error-responses/358/2 "2024-01-10T13:28:12Z")

</div>

I think adding an error output using `.errorOut` should do the trick:

```scala
  val createKeyDid: PublicEndpoint[Unit, Unit, CreatedDid, Any] = endpoint.get
    .in("did" / "create-key")
    .out(jsonBody[CreatedDid])
    .errorOut(jsonBody[ErrorResponse])

  val createKeyDidServerEndpoint: ServerEndpoint[Any, IO] =
    createKeyDid.serverLogic(_ =>
      val requestBody = KeyCreateDidRequestNew(method = "key")
      val url = Uri.parse(AppConfig.ssiKitCoreUrl ++ "/v1/did/create").getOrElse(uri"http://127.0.0.1:7000/v1/did/create")
      val response = client.send(basicRequest.post(url).body(requestBody).contentType("application/json"))

      if (response.code == StatusCode.Created) {
        println(s"Created did:key: ${response.body}")
        IO.pure(Right(CreatedDid(did = response.body.getOrElse("Problem reading did from response body"))))
      } else {
        IO.pure(Left("Problem creating key did"))
      }
    )

```

Also note that for the server logic, you should then use `.serverLogic`, not `.serverLogicSuccess` (which can’t signal errors), and wrap the result in a `Left`/`Right`.

See also the [error handling](https://tapir.softwaremill.com/en/latest/server/errors.html) docs

---

<div class="post-metadata">

**Author:** ![Elmer](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@Elmer](https://softwaremill.community/u/Elmer)\
**Post date:** [January 10, 2024, 2:11pm UTC](https://softwaremill.community/t/custom-error-responses/358/3 "2024-01-10T14:11:03Z")

</div>

Ah thank you! My main issue was I tried it with .serverLogicSuccess instead of .serverLogic.  
Your answer helped a lot!
