Skip to content

Commit

Permalink
release: 1.0.7 (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb authored Dec 30, 2024
2 parents 7bca13c + 93ab801 commit ffcafd8
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ kotestExtensionSpringVersion=1.1.3
testContainerVersion=1.19.3

### Netx ###
netxVersion=0.4.7
netxVersion=0.4.8

### Sentry ###
sentryVersion=4.4.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.gitanimals.render.app
package org.gitanimals.core

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
class AuthorizationException(message: String) : IllegalArgumentException(message)

val AUTHORIZATION_EXCEPTION = AuthorizationException("Authorization fail")
17 changes: 17 additions & 0 deletions src/main/kotlin/org/gitanimals/guild/controller/GuildController.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.gitanimals.guild.controller

import org.gitanimals.core.AuthorizationException
import org.gitanimals.core.FieldType
import org.gitanimals.guild.app.*
import org.gitanimals.guild.app.request.CreateGuildRequest
Expand All @@ -10,6 +11,7 @@ import org.gitanimals.guild.domain.GuildService
import org.gitanimals.guild.domain.SearchFilter
import org.gitanimals.guild.domain.extension.GuildFieldTypeExtension.isGuildField
import org.gitanimals.guild.domain.request.ChangeGuildRequest
import org.gitanimals.guild.controller.response.ErrorResponse
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*
Expand Down Expand Up @@ -146,4 +148,19 @@ class GuildController(
fun draw(
@PathVariable("guildId") guildId: Long,
) = drawGuildFacade.drawGuild(guildId)

@ExceptionHandler(IllegalArgumentException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse =
ErrorResponse.from(exception)

@ExceptionHandler(IllegalStateException::class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleIllegalArgumentException(exception: IllegalStateException): ErrorResponse =
ErrorResponse.from(exception)

@ExceptionHandler(AuthorizationException::class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
fun handleAuthorizationException(exception: AuthorizationException): ErrorResponse =
ErrorResponse.from(exception)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.gitanimals.guild.controller.response

data class ErrorResponse(
val message: String,
) {

companion object {
fun from(exception: Exception): ErrorResponse =
ErrorResponse(exception.message ?: exception.localizedMessage)
}
}
31 changes: 27 additions & 4 deletions src/main/kotlin/org/gitanimals/guild/infra/HttpClientConfigurer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class HttpClientConfigurer {

@Bean
fun identityApiHttpClient(): IdentityApi {
val restClient = RestClient.create("https://api.gitanimals.org")
val restClient = RestClient
.builder()
.defaultStatusHandler(httpClientErrorHandler())
.baseUrl("https://api.gitanimals.org")
.build()

val httpServiceProxyFactory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
Expand All @@ -26,14 +30,21 @@ class HttpClientConfigurer {

@Bean
fun renderApiHttpClient(): RenderApi {
val restClient = RestClient.create("https://render.gitanimals.org")
val restClient = RestClient
.builder()
.defaultStatusHandler(httpClientErrorHandler())
.baseUrl("https://render.gitanimals.org")
.build()

val httpServiceProxyFactory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build()

return httpServiceProxyFactory.createClient(RenderApi::class.java)
}

@Bean
fun httpClientErrorHandler(): HttpClientErrorHandler = HttpClientErrorHandler()
}

@Configuration
Expand All @@ -42,7 +53,11 @@ class TestHttpClientConfigurer {

@Bean
fun identityApiHttpClient(): IdentityApi {
val restClient = RestClient.create("http://localhost:8080")
val restClient = RestClient
.builder()
.defaultStatusHandler(httpClientErrorHandler())
.baseUrl("http://localhost:8080")
.build()

val httpServiceProxyFactory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
Expand All @@ -53,12 +68,20 @@ class TestHttpClientConfigurer {

@Bean
fun renderApiHttpClient(): RenderApi {
val restClient = RestClient.create("http://localhost:8080")
val restClient = RestClient
.builder()
.defaultStatusHandler(httpClientErrorHandler())
.baseUrl("http://localhost:8080")
.build()

val httpServiceProxyFactory = HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build()

return httpServiceProxyFactory.createClient(RenderApi::class.java)
}


@Bean
fun httpClientErrorHandler(): HttpClientErrorHandler = HttpClientErrorHandler()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.gitanimals.guild.infra

import org.gitanimals.core.AuthorizationException
import org.springframework.http.HttpStatus
import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.client.ResponseErrorHandler


class HttpClientErrorHandler : ResponseErrorHandler {

override fun hasError(response: ClientHttpResponse): Boolean {
return response.statusCode.isError
}

override fun handleError(response: ClientHttpResponse) {
val body = response.body.bufferedReader().use { it.readText() }
when {
response.statusCode.isSameCodeAs(HttpStatus.UNAUTHORIZED) ->
throw AuthorizationException(body)

response.statusCode.is4xxClientError ->
throw IllegalArgumentException(body)

response.statusCode.is5xxServerError ->
throw IllegalStateException(body)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class BackgroundController(
@RequestParam(name = "name") name: String,
) = userFacade.deleteField(token, FieldType.valueOf(name.uppercase()))

@ExceptionHandler(IllegalStateException::class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleIllegalArgumentException(exception: IllegalStateException): org.gitanimals.guild.controller.response.ErrorResponse =
org.gitanimals.guild.controller.response.ErrorResponse.from(exception)

@ExceptionHandler(IllegalArgumentException::class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
fun handleIllegalArgumentException(illegalArgumentException: IllegalArgumentException): ErrorResponse =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.gitanimals.render.controller

import org.gitanimals.core.IdGenerator
import org.gitanimals.render.app.AuthorizationException
import org.gitanimals.core.AuthorizationException
import org.gitanimals.render.app.UserFacade
import org.gitanimals.render.controller.request.AddMultiplyPersonaRequest
import org.gitanimals.render.controller.request.AddPersonaRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.gitanimals.render.controller

import org.gitanimals.core.PersonaType
import org.gitanimals.render.app.AuthorizationException
import org.gitanimals.core.AuthorizationException
import org.gitanimals.render.app.UserFacade
import org.gitanimals.render.app.request.MergePersonaRequest
import org.gitanimals.render.controller.response.ErrorResponse
Expand Down Expand Up @@ -82,6 +82,11 @@ class PersonaController(
fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse =
ErrorResponse.from(exception)

@ExceptionHandler(IllegalStateException::class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
fun handleIllegalArgumentException(exception: IllegalStateException): org.gitanimals.guild.controller.response.ErrorResponse =
org.gitanimals.guild.controller.response.ErrorResponse.from(exception)

@ExceptionHandler(AuthorizationException::class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
fun handleAuthorizationException(exception: AuthorizationException): ErrorResponse =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.gitanimals.render.infra

import org.gitanimals.render.app.AUTHORIZATION_EXCEPTION
import org.gitanimals.core.AUTHORIZATION_EXCEPTION
import org.gitanimals.render.app.IdentityApi
import org.springframework.http.HttpHeaders
import org.springframework.stereotype.Component
Expand Down

0 comments on commit ffcafd8

Please sign in to comment.