arrow-kt / arrow-kt/arrow

["Request"] Integration with Ktor StatusPage for failure handling

Open
#3,636 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Kotlin
Stars
6.6k
Forks
472
Avg merge
3d 49m
Merged PRs (30d)
4

Description

**What version are you currently using?**
2.1.2
**What would you like to see?**
First of all, I'm not entirely sure if this issue belongs in this repository or in the Ktor one. Please feel free to close it if it's out of scope for Arrow.

In Ktor, the [StatusPages plugin](https://ktor.io/docs/server-status-pages.html) allows centralized exception handling by automatically detecting exceptions:

```kotlin
install(StatusPages) {
exception { call, cause ->
call.respondText(text = "500: $cause" , status = HttpStatusCode.InternalServerError)
}
}
```
A similar mechanism could be incredibly helpful when using Arrow’s typed errors.
Currently, all errors must be handled locally ([here](https://slack-chats.kotlinlang.org/t/16776074/i-have-a-few-helpers-for-using-raise-with-ktor-should-i-open)'s an example I found online), which often leads to writing custom boilerplate to maintain the DRY principle.

A centralized way to handle typed errors would be highly practical especially in web applications as in this case, since strong typing could enable straightforward HTTP status code mapping.

The code that follows would be a dream of mine, though I'm unsure whether it's really feasible or how complex it would be to implement:
```kotlin
install(StatusPages) {
recover { call, error ->
call.respondText(text = "404: $error" , status = HttpStatusCode.NotFound)
}
}
```
I'm aware there may be significant limitations to making this kind of integration possible, particularly because the Ktor API is designed to be agnostic and shouldn't depend on the presence of Arrow constructs. That said, if this feature is of interest to the community, even a suboptimal solution could be greatly appreciated!

Thank you so much for all your work!

Contributor guide

Open the contributing guide

Research direction

Start by reviewing Ktor's StatusPages plugin documentation and the Arrow typed-error handling example linked in the issue. Determine whether this integration belongs in Arrow or Ktor and define its feasible scope; done means a decided, supported approach or an explicit out-of-scope conclusion.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.