Skip to content

Koriit/ktor-error-responses

Repository files navigation

Ktor Error Responses

Build CodeFactor ktlint

Maven Central GitHub

Warning
From version 0.4.0 all package names have been renamed to match new artifact group id.

Ktor Error Responses feature for HTTP APIs

Example

Installation
install(CallId) {
    header(HttpHeaders.XRequestId)
    generate { UUID.randomUUID().toString() }
    verify { it.isNotBlank() }
}

install(ErrorResponses) {
    handler<DefaultExceptionHandler> {
        // Domain
        register<IncorrectSchemaException>(BadRequest)
        register<NotSupportedException>(BadRequest)
        register<ItemNotFoundException>(NotFound)
        // Jackson
        registerReceive<JsonProcessingException>(BadRequest)
        //
        // registerSend...
    }
}
Generic 404
{
  "status": 404,
  "type": "Generic 404",
  "title": "Not Found",
  "detail": "404 Not Found",
  "instance": "ae6c1d55-38bf-4223-8562-a0e87e8b9908",
  "path": "/api/unknown/path",
  "timestamp": "2019-10-24T14:47:34.862+02:00"
}
Mapped error
{
  "status": 404,
  "type": "your.app.exceptions.ItemNotFound",
  "title": "Not Found",
  "detail": "Could not find entity: 123",
  "instance": "f7d4d3d7-910b-495a-b3b0-bce8ef869408",
  "path": "/api/my-entities/123",
  "timestamp": "2019-10-24T14:45:32.532+02:00"
}

Requirements

TODO Doc

Mapping

TODO Doc

Default Exception Handler

TODO Doc

API Error

TODO Doc

Experimental status

Error responses for Ktor uses some experimental Ktor API and therefore unfortunately there is no guarantee that there will no compatibility breaks between Ktor versions.