Skip to content

Commit

Permalink
Finalize error logic & structure in Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Apr 28, 2024
1 parent a225d4e commit 6e31710
Show file tree
Hide file tree
Showing 24 changed files with 273 additions and 54 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* Domain exception to throw when an internal error occurred in the backend.
*
* ## Examples
* - An unexpected exception has been caught.
*
* ## Logging
* This exception is logged as an error on the Backend side.
*/
open class BackendInternalException(
final override val message: String,
val originalException: Exception? = null,
) : Throwable(message) {
private val logger: Logger = LoggerFactory.getLogger(BackendInternalException::class.java)

init {
logger.error("BackendInternalException: $message")
originalException?.let { logger.error("${it::class.simpleName}: ${it.message}") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

/**
* Error code thrown when the request is invalid.
*
* ## Examples
* - The request has missing or wrongly-typed properties.
* - The request has valid properties but is made of illogical data.
*
* ## Logging
* The related exception is logged as a warning on the Backend side in order to track any unexpected behavior.
* It should definitely be logged on the Frontend side as an error.
*
* ## ⚠️ Important
* **Don't forget to mirror any update here in the corresponding Frontend enum.**
*/
enum class BackendRequestErrorCode {
/** Thrown when a request body property has an unexpected type. */
WRONG_REQUEST_BODY_PROPERTY_TYPE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* Domain exception to throw when a request is invalid.
*
* ## Examples
* - The request has missing or wrongly-typed properties.
* - The request has valid properties but is made of illogical data.
*
* ## Logging
* This exception is logged as a warning on the Backend side in order to track any unexpected behavior.
* It should definitely be logged on the Frontend side as an error.
*
* ## ⚠️ Important
* **Don't forget to mirror any update here in the corresponding Frontend enum.**
*/
open class BackendRequestException(
val code: BackendRequestErrorCode,
final override val message: String? = null,
val data: Any? = null,
) : Throwable(code.name) {
private val logger: Logger = LoggerFactory.getLogger(BackendRequestException::class.java)

init {
logger.warn("$code: ${message ?: "No message."}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

/**
* Error code thrown when the request is valid but the backend cannot process it.
*
* It's called "usage" because this request likely comes from an end-user action that's no longer valid
* which happens when their client data is not up-to-date with the backend.
*
* ## Examples
* - A user tries to create a resource that has already been created.
* - A user tries to delete a resource that doesn't exist anymore.
*
* ## Logging
* The related exception is NOT logged on the Backend side.
* It should NOT be logged on the Frontend side,
* it should rather display a comprehensible error message to the end-user.
*
* ### ⚠️ Important
* **Don't forget to mirror any update here in the corresponding Frontend enum.**
*/
enum class BackendUsageErrorCode {
/**
* Thrown when attempting to attach a mission to a reporting that has already a mission
* attached.
*/
CHILD_ALREADY_ATTACHED,

/** Thrown when attempting to archive an entity linked to non-archived child(ren). */
UNARCHIVED_CHILD,

/** Thrown when attempting to delete a mission that has actions created by other applications. */
EXISTING_MISSION_ACTION,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

import fr.gouv.cacem.monitorenv.domain.entities.ErrorCode

class BackendUsageException(val code: ErrorCode, val data: Any) : Throwable(code.name)
/**
* Domain exception to throw when a request is valid but the backend cannot process it.
*
* It's called "usage" because this request likely comes from an end-user action that's no longer valid
* which happens when their client data is not up-to-date with the backend.
*
* ## Examples
* - A user tries to create a resource that has already been created.
* - A user tries to delete a resource that doesn't exist anymore.
*
* ## Logging
* This exception is NOT logged on the Backend side.
* It should NOT be logged on the Frontend side,
* it should rather display a comprehensible error message to the end-user.
*/
open class BackendUsageException(
val code: BackendUsageErrorCode,
final override val message: String? = null,
val data: Any? = null,
) : Throwable(code.name)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

@Deprecated("Use `BackendUsageException` instead.")
class CouldNotArchiveException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

@Deprecated("Use `BackendUsageException` instead.")
class CouldNotDeleteException(message: String) : RuntimeException(message)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

@Deprecated("Use `BackendInternalException` or `BackendRequestException` instead (depending on the case).")
class EntityConversionException(message: String, cause: Throwable? = null) :
Throwable(message, cause)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

@Deprecated("Use `BackendUsageException` instead.")
class NotFoundException(message: String, cause: Throwable? = null) :
Throwable(message, cause)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package fr.gouv.cacem.monitorenv.domain.exceptions

/** Thrown when attempting to attach a reporting already attached to a mission. */
@Deprecated("Use `BackendUsageException` instead.")
class ReportingAlreadyAttachedException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
package fr.gouv.cacem.monitorenv.domain.use_cases.missions

import fr.gouv.cacem.monitorenv.config.UseCase
import fr.gouv.cacem.monitorenv.domain.entities.ErrorCode
import fr.gouv.cacem.monitorenv.domain.entities.mission.MissionSourceEnum
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode
import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageException
import fr.gouv.cacem.monitorenv.domain.repositories.IMissionRepository
import fr.gouv.cacem.monitorenv.domain.repositories.IReportingRepository
import org.slf4j.LoggerFactory
import java.time.ZonedDateTime
import java.util.UUID
import java.util.*

@UseCase
class DeleteMission(
Expand Down Expand Up @@ -40,8 +40,8 @@ class DeleteMission(
}

throw BackendUsageException(
ErrorCode.EXISTING_MISSION_ACTION,
errorSources,
code = BackendUsageErrorCode.EXISTING_MISSION_ACTION,
data = errorSources,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs

@Deprecated(
"Use either `BackendInternalErrorDataOutput`, `BackendRequestErrorDataOutput` or `BackendUsageErrorDataOutput` instead.",
)
class ApiError(val type: String) {
constructor(exception: Throwable) : this(exception.cause?.javaClass?.simpleName.toString())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs

/**
* Domain exception to throw when an internal error occurred in the backend.
*
* ## Examples
* - An unexpected exception has been caught.
*
* ## Logging
* The related exception is logged as an error on the Backend side.
*/
class BackendInternalErrorDataOutput {
val message: String = "An internal error occurred."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs

import fr.gouv.cacem.monitorenv.domain.exceptions.BackendRequestErrorCode

/**
* Error output to use when the request is invalid.
*
* ## Examples
* - The request has missing or wrongly-typed properties.
* - The request has valid properties but is made of illogical data.
*
* ## Logging
* The related exception is logged as a warning on the Backend side in order to track any unexpected behavior.
* It should definitely be logged on the Frontend side as an error.
*/
data class BackendRequestErrorDataOutput(
val code: BackendRequestErrorCode,
val data: Any? = null,
val message: String? = null,
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.gouv.cacem.monitorenv.infrastructure.api.adapters.bff.outputs

import fr.gouv.cacem.monitorenv.domain.exceptions.BackendUsageErrorCode

/**
* Error output to use when the request is valid but the backend cannot process it.
*
* It's called "usage" because this request likely comes from an end-user action that's no longer valid
* which happens when their client data is not up-to-date with the backend.
*
* ## Examples
* - A user tries to create a resource that has already been created.
* - A user tries to delete a resource that doesn't exist anymore.
*
* ## Logging
* The related exception is NOT logged on the Backend side.
* It should NOT be logged on the Frontend side,
* it should rather display a comprehensible error message to the end-user.
*/
data class BackendUsageErrorDataOutput(
val code: BackendUsageErrorCode,
val data: Any? = null,
val message: String? = null,
)
Loading

0 comments on commit 6e31710

Please sign in to comment.