Skip to content

Commit

Permalink
feat(oppijanumero): throw OppijaNotFoundException, when oppija is not…
Browse files Browse the repository at this point in the history
… found
  • Loading branch information
saku-koodari committed Dec 30, 2024
1 parent de81273 commit f0a913b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package fi.oph.kitu.oppijanumero

class OppijanumeroException(
open class OppijanumeroException(
val oppija: Oppija,
message: String,
val oppijanumeroServiceError: OppijanumeroServiceError? = null,
cause: Throwable? = null,
) : Throwable(message, cause)
) : Throwable(message, cause) {
class OppijaNotFoundException(
oppija: Oppija,
message: String = "Oppija not found from oppijanumero-service",
oppijanumeroServiceError: OppijanumeroServiceError? = null,
cause: Throwable? = null,
) : OppijanumeroException(oppija, message, oppijanumeroServiceError, cause)

class OppijaNotIdentifiedException(
oppija: Oppija,
message: String = "Oppija with oid ${oppija.henkilo_oid} is not identified in oppijanumero-service",
oppijanumeroServiceError: OppijanumeroServiceError? = null,
cause: Throwable? = null,
) : OppijanumeroException(oppija, message, oppijanumeroServiceError, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,20 @@ class OppijanumeroServiceImpl(
.sendRequest(httpRequest)
.getOrLogAndThrowCasException(event)

val body = tryConvertToOppijanumeroResponse<YleistunnisteHaeResponse>(oppija, stringResponse)

if (stringResponse.statusCode() == 404) {
throw OppijanumeroException(
oppija.withYleistunnisteHaeResponse(body),
"Oppija not found from oppijanumero-service",
)
throw OppijanumeroException.OppijaNotFoundException(oppija)
} else if (stringResponse.statusCode() != 200) {
throw OppijanumeroException(
oppija.withYleistunnisteHaeResponse(body),
oppija,
"Oppijanumero-service returned unexpected status code ${stringResponse.statusCode()}",
)
}

val body = tryConvertToOppijanumeroResponse<YleistunnisteHaeResponse>(oppija, stringResponse)

if (body.oppijanumero.isNullOrEmpty()) {
throw OppijanumeroException(
throw OppijanumeroException.OppijaNotIdentifiedException(
oppija.withYleistunnisteHaeResponse(body),
"Oppija is not identified in oppijanumero-service",
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class OppijanumeroServiceTests {
objectMapper = objectMapper,
)

assertThrows<OppijanumeroException> {
assertThrows<OppijanumeroException.OppijaNotIdentifiedException> {
oppijanumeroService.getOppijanumero(
Oppija(
"Magdalena Testi",
Expand Down Expand Up @@ -113,7 +113,7 @@ class OppijanumeroServiceTests {
objectMapper = objectMapper,
)

assertThrows<OppijanumeroException> {
assertThrows<OppijanumeroException.OppijaNotFoundException> {
oppijanumeroService.getOppijanumero(
Oppija(
"Magdalena Testi",
Expand Down

0 comments on commit f0a913b

Please sign in to comment.