Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable explicitApi flag #47

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ val build by tasks.named("build") {
dependsOn(securityTest, dataRestTest, txTest, awsTest)
}

kotlin {
explicitApi()
}

java {
withSourcesJar()
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/ekino/oss/errors/DefaultErrorCode.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.ekino.oss.errors

enum class DefaultErrorCode(
internal enum class DefaultErrorCode(
private val value: String
) {
NOT_FOUND("error.not_found"),
CONFLICT("error.conflict");

fun value(): String {
internal fun value(): String {
return this.value
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/ekino/oss/errors/ErrorBody.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.ekino.oss.errors

import java.time.Instant

data class ErrorBody(
public data class ErrorBody(
val status: Int,
val code: String,
val message: String,
Expand Down
20 changes: 10 additions & 10 deletions src/main/kotlin/com/ekino/oss/errors/ErrorsAutoConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,60 @@ import org.springframework.context.annotation.Configuration
@ConditionalOnWebApplication(type = SERVLET)
@AutoConfigureBefore(ErrorMvcAutoConfiguration::class)
@EnableConfigurationProperties(ErrorsProperties::class)
class ErrorsAutoConfiguration(
public class ErrorsAutoConfiguration(
@param:Value("\${spring.application.name}") private val applicationName: String,
private val properties: ErrorsProperties
) {

@Bean
fun coreExceptionHandler(): CoreExceptionHandler {
public fun coreExceptionHandler(): CoreExceptionHandler {
return object : CoreExceptionHandler(applicationName, properties) {}
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = ["org.springframework.security.authentication.AuthenticationManager"])
class SecurityHandlerConfiguration(
public class SecurityHandlerConfiguration(
@param:Value("\${spring.application.name}") private val applicationName: String,
private val properties: ErrorsProperties
) {
@Bean
fun securityExceptionHandler(): SecurityExceptionHandler {
public fun securityExceptionHandler(): SecurityExceptionHandler {
return object : SecurityExceptionHandler(applicationName, properties) {}
}
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = ["org.springframework.data.rest.core.config.RepositoryRestConfiguration"])
class DataRestHandlerConfiguration(
public class DataRestHandlerConfiguration(
@param:Value("\${spring.application.name}") private val applicationName: String,
private val properties: ErrorsProperties
) {
@Bean
fun dataRestExceptionHandler(): DataRestExceptionHandler {
public fun dataRestExceptionHandler(): DataRestExceptionHandler {
return object : DataRestExceptionHandler(applicationName, properties) {}
}
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = ["org.springframework.dao.DataIntegrityViolationException"])
class TxHandlerConfiguration(
public class TxHandlerConfiguration(
@param:Value("\${spring.application.name}") private val applicationName: String,
private val properties: ErrorsProperties
) {
@Bean
fun txExceptionHandler(): TxExceptionHandler {
public fun txExceptionHandler(): TxExceptionHandler {
return object : TxExceptionHandler(applicationName, properties) {}
}
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = ["software.amazon.awssdk.services.s3.model.NoSuchKeyException"])
class AwsHandlerConfiguration(
public class AwsHandlerConfiguration(
@param:Value("\${spring.application.name}") private val applicationName: String,
private val properties: ErrorsProperties
) {
@Bean
fun awsExceptionHandler(): AwsExceptionHandler {
public fun awsExceptionHandler(): AwsExceptionHandler {
return object : AwsExceptionHandler(applicationName, properties) {}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ekino.oss.errors

data class ValidationErrorBody(
public data class ValidationErrorBody(
val code: String,
val field: String? = null,
val message: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import com.ekino.oss.errors.ValidationErrorBody
import org.springframework.http.HttpStatus
import java.time.Instant

fun notFound(service: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun notFound(service: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(service, HttpStatus.NOT_FOUND, DefaultErrorCode.NOT_FOUND.value(), HttpStatus.NOT_FOUND.reasonPhrase,
devMessage, stacktrace, emptyList(), emptyList())
}

fun unavailable(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun unavailable(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(
service, HttpStatus.SERVICE_UNAVAILABLE, code, HttpStatus.SERVICE_UNAVAILABLE.reasonPhrase,
devMessage, stacktrace, emptyList(), emptyList()
)
}

fun unAuthorized(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun unAuthorized(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(service, HttpStatus.UNAUTHORIZED, code, HttpStatus.UNAUTHORIZED.reasonPhrase, devMessage, stacktrace, emptyList(), emptyList())
}

fun forbidden(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun forbidden(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(service, HttpStatus.FORBIDDEN, code, HttpStatus.FORBIDDEN.reasonPhrase, devMessage, stacktrace, emptyList(), emptyList())
}

@JvmOverloads
fun badRequest(
internal fun badRequest(
service: String,
code: String,
devMessage: String?,
Expand All @@ -38,18 +38,18 @@ fun badRequest(
return toError(service, HttpStatus.BAD_REQUEST, code, HttpStatus.BAD_REQUEST.reasonPhrase, devMessage, stacktrace, errors, globalErrors)
}

fun methodNotAllowed(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun methodNotAllowed(service: String, code: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(
service, HttpStatus.METHOD_NOT_ALLOWED, code, HttpStatus.METHOD_NOT_ALLOWED.reasonPhrase, devMessage, stacktrace, emptyList(), emptyList()
)
}

fun conflict(service: String, devMessage: String?, stacktrace: String): ErrorBody {
internal fun conflict(service: String, devMessage: String?, stacktrace: String): ErrorBody {
return toError(service, HttpStatus.CONFLICT, DefaultErrorCode.CONFLICT.value(), HttpStatus.CONFLICT.reasonPhrase,
devMessage, stacktrace, emptyList(), emptyList())
}

fun preconditionFailed(service: String, devMessage: String, stacktrace: String): ErrorBody {
internal fun preconditionFailed(service: String, devMessage: String, stacktrace: String): ErrorBody {
return toError(
service,
HttpStatus.PRECONDITION_FAILED,
Expand All @@ -61,20 +61,26 @@ fun preconditionFailed(service: String, devMessage: String, stacktrace: String):
emptyList())
}

fun conflict(service: String, devMessage: String, stacktrace: String, errors: List<ValidationErrorBody>): ErrorBody {
internal fun conflict(service: String, devMessage: String, stacktrace: String, errors: List<ValidationErrorBody>): ErrorBody {
return toError(
service, HttpStatus.CONFLICT, DefaultErrorCode.CONFLICT.value(), HttpStatus.CONFLICT.reasonPhrase, devMessage, stacktrace, errors, emptyList()
)
}

fun unprocessableEntity(service: String, code: String, devMessage: String, stacktrace: String, errors: List<ValidationErrorBody>): ErrorBody {
internal fun unprocessableEntity(
service: String,
code: String,
devMessage: String,
stacktrace: String,
errors: List<ValidationErrorBody>
): ErrorBody {
return toError(
service, HttpStatus.UNPROCESSABLE_ENTITY, code, HttpStatus.UNPROCESSABLE_ENTITY.reasonPhrase, devMessage, stacktrace, errors, emptyList()
)
}

@JvmOverloads
fun defaultError(
internal fun defaultError(
service: String,
httpStatus: HttpStatus,
code: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import javax.servlet.http.HttpServletRequest
*/
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
abstract class AwsExceptionHandler(
public abstract class AwsExceptionHandler(
private val applicationName: String,
private val properties: ErrorsProperties
) {
private val log by logger()

@ExceptionHandler(NoSuchKeyException::class)
fun handleNoSuchKeyException(req: HttpServletRequest, e: NoSuchKeyException): ResponseEntity<ErrorBody> {
public fun handleNoSuchKeyException(req: HttpServletRequest, e: NoSuchKeyException): ResponseEntity<ErrorBody> {
log.debug("Object not found on S3", e)
return notFound(req.toServiceName(applicationName), e.message, e.toStacktrace(properties.displayFullStacktrace)).toErrorResponse()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ import javax.validation.ConstraintViolationException
*/
@RestControllerAdvice
@Order(Ordered.LOWEST_PRECEDENCE)
abstract class CoreExceptionHandler(
public abstract class CoreExceptionHandler(
private val applicationName: String,
private val properties: ErrorsProperties
) {
private val log by logger()

@ExceptionHandler(ConnectException::class)
fun handleUnavailableServiceException(req: HttpServletRequest, e: Exception): ResponseEntity<ErrorBody> {
public fun handleUnavailableServiceException(req: HttpServletRequest, e: Exception): ResponseEntity<ErrorBody> {
log.error("Unavailable service : ", e)
return unavailable(
req.toServiceName(applicationName), "error.unavailable", e.message, e.toStacktrace(properties.displayFullStacktrace)
).toErrorResponse()
}

@ExceptionHandler(MethodArgumentNotValidException::class)
fun handleValidationException(req: HttpServletRequest, e: MethodArgumentNotValidException): ResponseEntity<ErrorBody> {
public fun handleValidationException(req: HttpServletRequest, e: MethodArgumentNotValidException): ResponseEntity<ErrorBody> {
return prepareValidationResponse(req, e, e.bindingResult)
}

@ExceptionHandler(BindException::class)
fun handleBindException(req: HttpServletRequest, e: BindException): ResponseEntity<ErrorBody> {
public fun handleBindException(req: HttpServletRequest, e: BindException): ResponseEntity<ErrorBody> {
return prepareValidationResponse(req, e, e.bindingResult)
}

@ExceptionHandler(NestedRuntimeException::class)
fun handleNestedRuntimeException(req: HttpServletRequest, e: NestedRuntimeException): ResponseEntity<ErrorBody> {
public fun handleNestedRuntimeException(req: HttpServletRequest, e: NestedRuntimeException): ResponseEntity<ErrorBody> {
log.error("Nested runtime exception : ", e)

val cause = e.mostSpecificCause
Expand All @@ -71,7 +71,7 @@ abstract class CoreExceptionHandler(
}

@ExceptionHandler(ConstraintViolationException::class)
fun handleConstraintViolationException(req: HttpServletRequest, e: ConstraintViolationException): ResponseEntity<ErrorBody> {
public fun handleConstraintViolationException(req: HttpServletRequest, e: ConstraintViolationException): ResponseEntity<ErrorBody> {
log.debug("Constraint violation errors : ", e)

val errors = e.constraintViolations?.map { it.toValidationErrorBody() } ?: emptyList()
Expand All @@ -82,45 +82,48 @@ abstract class CoreExceptionHandler(
}

@ExceptionHandler(HttpMessageConversionException::class)
fun handleMessageNotReadableException(req: HttpServletRequest, e: HttpMessageConversionException): ResponseEntity<ErrorBody> {
public fun handleMessageNotReadableException(req: HttpServletRequest, e: HttpMessageConversionException): ResponseEntity<ErrorBody> {
log.debug("Message not readable : ", e)
return badRequest(
req.toServiceName(applicationName), "error.not_readable_json", e.message, e.toStacktrace(properties.displayFullStacktrace)
).toErrorResponse()
}

@ExceptionHandler(MethodArgumentTypeMismatchException::class)
fun handleArgumentTypeMismatchException(req: HttpServletRequest, e: MethodArgumentTypeMismatchException): ResponseEntity<ErrorBody> {
public fun handleArgumentTypeMismatchException(req: HttpServletRequest, e: MethodArgumentTypeMismatchException): ResponseEntity<ErrorBody> {
log.debug("Argument type mismatch : ", e)
return badRequest(
req.toServiceName(applicationName), "error.argument_type_mismatch", e.message, e.toStacktrace(properties.displayFullStacktrace)
).toErrorResponse()
}

@ExceptionHandler(MissingServletRequestParameterException::class)
fun handleMissingServletRequestParameterException(req: HttpServletRequest, e: MissingServletRequestParameterException): ResponseEntity<ErrorBody> {
public fun handleMissingServletRequestParameterException(
req: HttpServletRequest,
e: MissingServletRequestParameterException
): ResponseEntity<ErrorBody> {
log.debug("Missing parameter : ", e)
return badRequest(
req.toServiceName(applicationName), "error.missing_parameter", e.message, e.toStacktrace(properties.displayFullStacktrace)
).toErrorResponse()
}

@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
fun handleMethodNotSupportedException(req: HttpServletRequest, e: HttpRequestMethodNotSupportedException): ResponseEntity<ErrorBody> {
public fun handleMethodNotSupportedException(req: HttpServletRequest, e: HttpRequestMethodNotSupportedException): ResponseEntity<ErrorBody> {
log.debug("Method not supported : ", e)
return methodNotAllowed(
req.toServiceName(applicationName), "error.method_not_allowed", e.message, e.toStacktrace(properties.displayFullStacktrace)
).toErrorResponse()
}

@ExceptionHandler(NoHandlerFoundException::class)
fun handleNoHandlerFoundException(req: HttpServletRequest, e: NoHandlerFoundException): ResponseEntity<ErrorBody> {
public fun handleNoHandlerFoundException(req: HttpServletRequest, e: NoHandlerFoundException): ResponseEntity<ErrorBody> {
log.trace("Resource not found : ", e)
return notFound(req.toServiceName(applicationName), e.message, e.toStacktrace(properties.displayFullStacktrace)).toErrorResponse()
}

@ExceptionHandler(Throwable::class)
fun handleException(req: HttpServletRequest, e: Throwable): ResponseEntity<ErrorBody> {
public fun handleException(req: HttpServletRequest, e: Throwable): ResponseEntity<ErrorBody> {
val responseStatus = AnnotationUtils.findAnnotation(e.javaClass, ResponseStatus::class.java)

if (responseStatus == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import javax.servlet.http.HttpServletRequest
*/
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
abstract class DataRestExceptionHandler(
public abstract class DataRestExceptionHandler(
private val applicationName: String,
private val properties: ErrorsProperties
) {
private val log by logger()

@ExceptionHandler(ResourceNotFoundException::class)
fun handleResourceNotFoundException(req: HttpServletRequest, e: ResourceNotFoundException): ResponseEntity<ErrorBody> {
public fun handleResourceNotFoundException(req: HttpServletRequest, e: ResourceNotFoundException): ResponseEntity<ErrorBody> {
log.trace("Resource not found : ", e)
return notFound(req.toServiceName(applicationName), e.message, e.toStacktrace(properties.displayFullStacktrace)).toErrorResponse()
}

@ExceptionHandler(RepositoryConstraintViolationException::class)
fun handleRepositoryConstraintViolationException(
public fun handleRepositoryConstraintViolationException(
e: RepositoryConstraintViolationException,
req: HttpServletRequest
): ResponseEntity<ErrorBody> {
Expand All @@ -50,7 +50,7 @@ abstract class DataRestExceptionHandler(
}

@ExceptionHandler(DataIntegrityViolationException::class)
fun handleConflict(e: DataIntegrityViolationException, req: HttpServletRequest): ResponseEntity<ErrorBody> {
public fun handleConflict(e: DataIntegrityViolationException, req: HttpServletRequest): ResponseEntity<ErrorBody> {
log.debug("Database conflict : ", e)

val cause = e.cause
Expand Down
Loading