Skip to content

Commit 105890c

Browse files
committed
Added Problem and handling of missing access and not found.
1 parent 5ecc6a1 commit 105890c

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ val springVersion = "2.5.5"
66
val testContainersVersion = "1.17.3"
77
val springDocVersion = "1.6.11"
88
val tokenValidationVersion = "2.1.4"
9+
val problemSpringWebStartVersion = "0.27.0"
910

1011
repositories {
1112
mavenCentral()
@@ -30,6 +31,8 @@ dependencies {
3031
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
3132
implementation("org.springframework.boot:spring-boot-starter-validation")
3233

34+
implementation("org.zalando:problem-spring-web-starter:$problemSpringWebStartVersion")
35+
3336
implementation("no.nav.security:token-validation-spring:$tokenValidationVersion")
3437

3538
implementation("org.springdoc:springdoc-openapi-ui:$springDocVersion")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package no.nav.klage.document.config
2+
3+
import no.nav.klage.document.exceptions.MissingAccessException
4+
import no.nav.klage.document.util.getLogger
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.web.bind.annotation.ControllerAdvice
7+
import org.springframework.web.bind.annotation.ExceptionHandler
8+
import org.springframework.web.context.request.NativeWebRequest
9+
import org.zalando.problem.Problem
10+
import org.zalando.problem.Status
11+
import org.zalando.problem.spring.web.advice.AdviceTrait
12+
import org.zalando.problem.spring.web.advice.ProblemHandling
13+
import javax.persistence.EntityNotFoundException
14+
15+
@ControllerAdvice
16+
class ProblemHandlingControllerAdvice : OurOwnExceptionAdviceTrait, ProblemHandling
17+
18+
interface OurOwnExceptionAdviceTrait : AdviceTrait {
19+
20+
companion object {
21+
@Suppress("JAVA_CLASS_ON_COMPANION")
22+
private val logger = getLogger(javaClass.enclosingClass)
23+
}
24+
25+
@ExceptionHandler
26+
fun handleEntityNotFound(
27+
ex: EntityNotFoundException,
28+
request: NativeWebRequest
29+
): ResponseEntity<Problem> =
30+
create(Status.NOT_FOUND, ex, request)
31+
32+
@ExceptionHandler
33+
fun handleMissingAccess(
34+
ex: MissingAccessException,
35+
request: NativeWebRequest
36+
): ResponseEntity<Problem> =
37+
create(Status.FORBIDDEN, ex, request)
38+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package no.nav.klage.document.exceptions
2+
3+
class MissingAccessException(msg: String) : RuntimeException(msg)

src/main/kotlin/no/nav/klage/document/service/CommentService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package no.nav.klage.document.service
22

33
import no.nav.klage.document.domain.Comment
4+
import no.nav.klage.document.exceptions.MissingAccessException
45
import no.nav.klage.document.repositories.CommentRepository
56
import org.springframework.stereotype.Service
67
import org.springframework.transaction.annotation.Transactional
@@ -57,7 +58,7 @@ class CommentService(private val commentRepository: CommentRepository) {
5758
fun setCommentText(commentId: UUID, text: String, loggedInIdent: String): Comment {
5859
val comment = commentRepository.getReferenceById(commentId)
5960
if (comment.authorIdent != loggedInIdent) {
60-
throw RuntimeException("Not allowed to modify others comment")
61+
throw MissingAccessException("Not allowed to modify others comment")
6162
}
6263
comment.text = text
6364
comment.modified = LocalDateTime.now()
@@ -67,7 +68,7 @@ class CommentService(private val commentRepository: CommentRepository) {
6768
fun deleteComment(commentId: UUID, loggedInIdent: String) {
6869
val comment = commentRepository.getReferenceById(commentId)
6970
if (comment.authorIdent != loggedInIdent) {
70-
throw RuntimeException("Not allowed to delete others comment")
71+
throw MissingAccessException("Not allowed to delete others comment")
7172
}
7273
commentRepository.delete(comment)
7374
}

0 commit comments

Comments
 (0)