Skip to content

Commit

Permalink
feat: Create custom global error exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bouceka committed Nov 2, 2022
1 parent b0a195f commit b7d8773
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
29 changes: 29 additions & 0 deletions team-srv/src/main/kotlin/com/bouceka/exceptions/GlobalException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bouceka.exceptions

import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.MutableHttpResponse
import io.micronaut.http.annotation.Produces
import io.micronaut.http.server.exceptions.ExceptionHandler
import io.micronaut.http.server.exceptions.response.ErrorContext
import io.micronaut.http.server.exceptions.response.ErrorResponseProcessor
import jakarta.inject.Singleton

class GlobalException(override val message: String?, val httpResponse: MutableHttpResponse<Any>) : RuntimeException()

@Produces
@Singleton
@Requires(classes = [RuntimeException::class])
class GlobalExceptionHandler(private val errorResponseProcessor: ErrorResponseProcessor<Any>) :
ExceptionHandler<GlobalException, HttpResponse<*>> {

override fun handle(request: HttpRequest<*>, exception: GlobalException): HttpResponse<*> {
return errorResponseProcessor.processResponse(
ErrorContext.builder(request)
.cause(exception)
.errorMessage(exception.message)
.build(), exception.httpResponse) //
}
}

10 changes: 6 additions & 4 deletions team-srv/src/main/kotlin/com/bouceka/service/TeamService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package com.bouceka.service

import com.bouceka.dto.TeamDto
import com.bouceka.entity.TeamEntity
import com.bouceka.exceptions.GlobalException
import com.bouceka.exceptions.TeamNotFound
import com.bouceka.repository.TeamRepository
import io.micronaut.http.HttpResponse
import io.micronaut.http.annotation.Body
import jakarta.inject.Singleton
import org.bson.types.ObjectId
import java.net.http.HttpResponse
import java.util.*

@Singleton
Expand Down Expand Up @@ -40,7 +41,7 @@ class TeamService(private val teamRepository: TeamRepository) {
val foundTeam = teamRepository.findById(id)

if (foundTeam.isEmpty)
throw ClassNotFoundException("Team with id $id was not found")
throw GlobalException("Tam not found", HttpResponse.badRequest())

This comment has been minimized.

Copy link
@ash-teach

ash-teach Nov 10, 2022

Spelling error. "Team" not found. :)

This comment has been minimized.

Copy link
@bouceka

bouceka Nov 10, 2022

Author Owner

I know, I fixed it in one of the following commits 😥


return teamRepository.update(
TeamEntity(
Expand All @@ -59,9 +60,10 @@ class TeamService(private val teamRepository: TeamRepository) {
}

fun delete(id: String): Optional<TeamEntity> {
val foundTeam = teamRepository.findById(id)
if (!ObjectId.isValid(id)) throw GlobalException("Invalid Object Id", HttpResponse.badRequest())

if (foundTeam.isEmpty)
val foundTeam = teamRepository.findById(id)
if (teamRepository.findById(id).isEmpty)
throw TeamNotFound()

teamRepository.deleteById(id)
Expand Down

0 comments on commit b7d8773

Please sign in to comment.