-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[428] Better error handling in Xef Server (#430)
* added exceptions handler management in server * removed unnecessary response
- Loading branch information
Showing
10 changed files
with
106 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
server/src/main/kotlin/com/xebia/functional/xef/server/exceptions/ExceptionsHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.xebia.functional.xef.server.exceptions | ||
|
||
import com.xebia.functional.xef.server.models.exceptions.XefExceptions | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.plugins.statuspages.* | ||
import io.ktor.server.response.* | ||
|
||
fun Application.exceptionsHandler() { | ||
install(StatusPages) { | ||
exception<Throwable> { call, cause -> | ||
if (cause is XefExceptions) { | ||
call.manageException(cause) | ||
} else { | ||
call.respond(HttpStatusCode.InternalServerError, cause.localizedMessage ?: "Unexpected error") | ||
} | ||
} | ||
status(HttpStatusCode.NotFound) { call, status -> | ||
call.respondText(text = "404: Page Not Found", status = status) | ||
} | ||
} | ||
} | ||
|
||
suspend fun ApplicationCall.manageException(cause: XefExceptions) { | ||
when(cause) { | ||
is XefExceptions.ValidationException -> this.respond(HttpStatusCode.BadRequest, cause.message) | ||
is XefExceptions.AuthorizationException -> this.respond(HttpStatusCode.Unauthorized) | ||
is XefExceptions.OrganizationsException -> this.respond(HttpStatusCode.BadRequest, cause.message) | ||
is XefExceptions.UserException -> this.respond(HttpStatusCode.BadRequest, cause.message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
server/src/main/kotlin/com/xebia/functional/xef/server/models/exceptions/Exceptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.xebia.functional.xef.server.models.exceptions | ||
|
||
sealed class XefExceptions( | ||
override val message: String | ||
): Throwable() { | ||
class ValidationException(override val message: String): XefExceptions(message) | ||
class OrganizationsException(override val message: String): XefExceptions(message) | ||
class AuthorizationException(override val message: String): XefExceptions(message) | ||
class UserException(override val message: String): XefExceptions(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters