Add specific alteration error management.

This commit is contained in:
Thomas Andres Gomez 2025-04-01 22:18:55 +02:00
parent 6213d5ac15
commit f94a530621
48 changed files with 606 additions and 511 deletions

View file

@ -1,8 +1,14 @@
package com.pixelized.server.lwa.model.alteration
import com.pixelized.server.lwa.server.exception.BusinessException
import com.pixelized.server.lwa.server.exception.FileReadException
import com.pixelized.server.lwa.server.exception.FileWriteException
import com.pixelized.server.lwa.server.exception.JsonCodingException
import com.pixelized.server.lwa.server.exception.JsonConversionException
import com.pixelized.shared.lwa.model.alteration.Alteration
import com.pixelized.shared.lwa.model.alteration.AlterationJson
import com.pixelized.shared.lwa.model.alteration.AlterationJsonFactory
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.utils.PathProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -93,8 +99,9 @@ class AlterationStore(
val file = alterationFile(id = json.id)
// Guard case on update alteration
if (create && file.exists()) {
val root = Exception("Alteration already exist, creation is impossible.")
throw BusinessException(root = root)
throw BusinessException(
message = "Alteration already exist, creation is impossible.",
)
}
// Transform the json into the model.
val alteration = try {
@ -102,6 +109,18 @@ class AlterationStore(
} catch (exception: Exception) {
throw JsonConversionException(root = exception)
}
if (alteration.id.isEmpty()) {
throw BusinessException(
message = "Alteration 'id' is a mandatory field.",
code = APIResponse.ErrorCode.AlterationId,
)
}
if (alteration.metadata.name.isEmpty()) {
throw BusinessException(
message = "Alteration 'name' is a mandatory field.",
code = APIResponse.ErrorCode.AlterationName,
)
}
// Encode the json into a string.
val data = try {
this.json.encodeToString(json)
@ -139,13 +158,15 @@ class AlterationStore(
val file = alterationFile(id = id)
// Guard case on the file existence.
if (file.exists().not()) {
val root = Exception("Alteration doesn't not exist, deletion is impossible.")
throw BusinessException(root = root)
throw BusinessException(
message = "Alteration doesn't not exist, deletion is impossible.",
)
}
// Guard case on the file deletion
if (file.delete().not()) {
val root = Exception("Alteration file have not been deleted for unknown reason.")
throw BusinessException(root = root)
throw BusinessException(
message = "Alteration file have not been deleted for unknown reason.",
)
}
// Update the data model with the deleted alteration.
alterationFlow.update { alterations ->
@ -162,11 +183,4 @@ class AlterationStore(
private fun alterationFile(id: String): File {
return File("${pathProvider.alterationsPath()}${id}.json")
}
sealed class AlterationStoreException(root: Exception) : Exception(root)
class JsonConversionException(root: Exception) : AlterationStoreException(root)
class JsonCodingException(root: Exception) : AlterationStoreException(root)
class BusinessException(root: Exception) : AlterationStoreException(root)
class FileWriteException(root: Exception) : AlterationStoreException(root)
class FileReadException(root: Exception) : AlterationStoreException(root)
}

View file

@ -1,5 +1,6 @@
package com.pixelized.server.lwa.model.campaign
import com.pixelized.server.lwa.server.exception.BusinessException
import com.pixelized.shared.lwa.model.campaign.Campaign
import com.pixelized.shared.lwa.model.campaign.CampaignJson
import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonFactory
@ -43,8 +44,7 @@ class CampaignService(
) {
// Check if the character is already in the campaign.
if (campaign.instances.contains(characterSheetId)) {
val root = Exception("Character with id:$characterSheetId is already in the campaign.")
throw CampaignStore.BusinessException(root = root)
throw BusinessException(message = "Character with id:$characterSheetId is already in the campaign.")
}
// Update the corresponding instance
val characters = campaign.characters.toMutableSet().also {
@ -62,8 +62,7 @@ class CampaignService(
) {
// Check if the character is already in the campaign.
if (campaign.instances.contains(characterSheetId)) {
val root = Exception("Character with id:$characterSheetId is already in the campaign.")
throw CampaignStore.BusinessException(root = root)
throw BusinessException(message = "Character with id:$characterSheetId is already in the campaign.")
}
// Update the corresponding instance
val characters = campaign.npcs.toMutableSet().also {
@ -81,8 +80,7 @@ class CampaignService(
) {
// Check if the character is in the campaign.
if (campaign.characters.contains(characterSheetId).not()) {
val root = Exception("Character with id:$characterSheetId is not in the party.")
throw CampaignStore.BusinessException(root = root)
throw BusinessException(message = "Character with id:$characterSheetId is not in the party.")
}
// Update the corresponding instance
val characters = campaign.characters.toMutableSet().also {
@ -100,8 +98,7 @@ class CampaignService(
) {
// Check if the character is in the campaign.
if (campaign.npcs.contains(characterSheetId).not()) {
val root = Exception("Character with id:$characterSheetId is not in the npcs.")
throw CampaignStore.BusinessException(root = root)
throw BusinessException(message = "Character with id:$characterSheetId is not in the npcs.")
}
// Update the corresponding instance
val characters = campaign.npcs.toMutableSet().also {

View file

@ -1,5 +1,9 @@
package com.pixelized.server.lwa.model.campaign
import com.pixelized.server.lwa.server.exception.FileReadException
import com.pixelized.server.lwa.server.exception.FileWriteException
import com.pixelized.server.lwa.server.exception.JsonCodingException
import com.pixelized.server.lwa.server.exception.JsonConversionException
import com.pixelized.shared.lwa.model.campaign.Campaign
import com.pixelized.shared.lwa.model.campaign.CampaignJson
import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonFactory
@ -108,11 +112,4 @@ class CampaignStore(
private fun campaignFile(): File {
return File("${pathProvider.campaignPath()}campaign.json")
}
sealed class CampaignStoreException(root: Exception) : Exception(root)
class JsonConversionException(root: Exception) : CampaignStoreException(root)
class JsonCodingException(root: Exception) : CampaignStoreException(root)
class BusinessException(root: Exception) : CampaignStoreException(root)
class FileWriteException(root: Exception) : CampaignStoreException(root)
class FileReadException(root: Exception) : CampaignStoreException(root)
}

View file

@ -1,8 +1,14 @@
package com.pixelized.server.lwa.model.character
import com.pixelized.server.lwa.server.exception.BusinessException
import com.pixelized.server.lwa.server.exception.FileReadException
import com.pixelized.server.lwa.server.exception.FileWriteException
import com.pixelized.server.lwa.server.exception.JsonCodingException
import com.pixelized.server.lwa.server.exception.JsonConversionException
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheet
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson
import com.pixelized.shared.lwa.model.characterSheet.factory.CharacterSheetJsonFactory
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.utils.PathProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -93,8 +99,7 @@ class CharacterSheetStore(
val file = characterSheetFile(id = sheet.id)
// Guard case on update alteration
if (create && file.exists()) {
val root = Exception("Character already exist, creation is impossible.")
throw BusinessException(root = root)
throw BusinessException(message = "Character already exist, creation is impossible.")
}
// Transform the json into the model.
val json = try {
@ -139,13 +144,16 @@ class CharacterSheetStore(
val file = characterSheetFile(id = characterSheetId)
// Guard case on the file existence.
if (file.exists().not()) {
val root = Exception("Character file with id:$characterSheetId doesn't not exist.")
throw BusinessException(root = root)
throw BusinessException(
message = "Character file with id:$characterSheetId doesn't not exist.",
code = APIResponse.ErrorCode.CharacterSheetId
)
}
// Guard case on the file deletion
if (file.delete().not()) {
val root = Exception("Character file have not been deleted for unknown reason.")
throw BusinessException(root = root)
throw BusinessException(
message = "Character file have not been deleted for unknown reason.",
)
}
// Update the data model with the deleted character.
characterSheetsFlow.update { characters ->
@ -158,11 +166,4 @@ class CharacterSheetStore(
private fun characterSheetFile(id: String): File {
return File("${pathProvider.characterStorePath()}${id}.json")
}
sealed class CharacterSheetStoreException(root: Exception) : Exception(root)
class JsonConversionException(root: Exception) : CharacterSheetStoreException(root)
class JsonCodingException(root: Exception) : CharacterSheetStoreException(root)
class BusinessException(root: Exception) : CharacterSheetStoreException(root)
class FileWriteException(root: Exception) : CharacterSheetStoreException(root)
class FileReadException(root: Exception) : CharacterSheetStoreException(root)
}

View file

@ -1,5 +1,8 @@
package com.pixelized.server.lwa.model.tag
import com.pixelized.server.lwa.server.exception.FileReadException
import com.pixelized.server.lwa.server.exception.FileWriteException
import com.pixelized.server.lwa.server.exception.JsonConversionException
import com.pixelized.shared.lwa.model.tag.TagJson
import com.pixelized.shared.lwa.utils.PathProvider
import kotlinx.coroutines.CoroutineScope
@ -100,9 +103,4 @@ class TagStore(
private fun characterFile() = File("${pathProvider.tagsPath()}$CHARACTER.json")
private fun alterationFile() = File("${pathProvider.tagsPath()}$ALTERATION.json")
sealed class TagStoreException(root: Exception) : Exception(root)
class JsonConversionException(root: Exception) : TagStoreException(root)
class FileWriteException(root: Exception) : TagStoreException(root)
class FileReadException(root: Exception) : TagStoreException(root)
}

View file

@ -0,0 +1,10 @@
package com.pixelized.server.lwa.server.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
class BusinessException(
message: String,
val code: APIResponse.ErrorCode? = null,
) : ServerException(
root = Exception(message)
)

View file

@ -0,0 +1,3 @@
package com.pixelized.server.lwa.server.exception
class FileReadException(root: Exception) : ServerException(root)

View file

@ -0,0 +1,3 @@
package com.pixelized.server.lwa.server.exception
class FileWriteException(root: Exception) : ServerException(root)

View file

@ -0,0 +1,3 @@
package com.pixelized.server.lwa.server.exception
class JsonCodingException(root: Exception) : ServerException(root)

View file

@ -0,0 +1,3 @@
package com.pixelized.server.lwa.server.exception
class JsonConversionException(root: Exception) : ServerException(root)

View file

@ -0,0 +1,4 @@
package com.pixelized.server.lwa.server.exception
class MissingParameterException(name: String) :
ServerException(root = Exception("Missing '$name' parameter."))

View file

@ -0,0 +1,3 @@
package com.pixelized.server.lwa.server.exception
sealed class ServerException(root: Exception) : Exception(root)

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.alteration
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.alterationId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.deleteAlteration(): suspend RoutingContext.() -> Unit {
alterationId = alterationId,
),
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception,
)
}
}

View file

@ -2,6 +2,7 @@ package com.pixelized.server.lwa.server.rest.alteration
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.alterationId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -22,11 +23,8 @@ fun Engine.getAlteration(): suspend RoutingContext.() -> Unit {
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception,
)
}
}

View file

@ -1,6 +1,7 @@
package com.pixelized.server.lwa.server.rest.alteration
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -14,11 +15,8 @@ fun Engine.getAlterationTags(): suspend RoutingContext.() -> Unit {
),
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception,
)
}
}

View file

@ -1,6 +1,7 @@
package com.pixelized.server.lwa.server.rest.alteration
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -14,11 +15,8 @@ fun Engine.getAlterations(): suspend RoutingContext.() -> Unit {
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception,
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.alteration
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.create
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.model.alteration.AlterationJson
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation
@ -29,19 +29,9 @@ fun Engine.putAlteration(): suspend RoutingContext.() -> Unit {
alterationId = form.id,
),
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception,
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.removeCampaignCharacter(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.removeCampaignNpc(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,6 +1,7 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -14,11 +15,8 @@ fun Engine.getCampaign(): suspend RoutingContext.() -> Unit {
),
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.putCampaignCharacter(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.putCampaignNpc(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,7 +1,7 @@
package com.pixelized.server.lwa.server.rest.campaign
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.model.campaign.CampaignJsonV2
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
@ -30,19 +30,9 @@ fun Engine.putCampaignScene(): suspend RoutingContext.() -> Unit {
name = scene.name,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation
import io.ktor.server.response.respond
@ -27,19 +27,9 @@ fun Engine.deleteCharacter(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId,
),
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -22,19 +22,9 @@ fun Engine.getCharacter(): suspend RoutingContext.() -> Unit {
data = characterSheet,
),
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,6 +1,7 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -14,11 +15,8 @@ fun Engine.getCharacterTags(): suspend RoutingContext.() -> Unit {
),
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,6 +1,7 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext
@ -14,11 +15,8 @@ fun Engine.getCharacters(): suspend RoutingContext.() -> Unit {
),
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,8 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.create
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.model.characterSheet.CharacterSheetJson
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation
@ -28,19 +28,9 @@ fun Engine.putCharacter(): suspend RoutingContext.() -> Unit {
characterSheetId = form.id,
),
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,10 +1,10 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.active
import com.pixelized.server.lwa.utils.extentions.alterationId
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent
import io.ktor.server.response.respond
@ -35,19 +35,9 @@ fun Engine.putCharacterAlteration(): suspend RoutingContext.() -> Unit {
active = active,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,9 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.server.exception.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent
import io.ktor.server.response.respond
@ -14,10 +15,7 @@ fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit {
// get the query parameter
val characterSheetId = call.queryParameters.characterSheetId
val damage = call.queryParameters["damage"]?.toIntOrNull()
?: throw MissingParameterException(
name = "damage",
errorCode = APIResponse.MISSING_DAMAGE
)
?: throw MissingParameterException(name = "damage")
// fetch the character sheet
val characterSheet = characterService.character(characterSheetId)
?: error("CharacterSheet with id:$characterSheetId not found.")
@ -38,19 +36,9 @@ fun Engine.putCharacterDamage(): suspend RoutingContext.() -> Unit {
damage = damage,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,9 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.server.exception.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent
import io.ktor.server.response.respond
@ -14,10 +15,7 @@ fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit {
// get the query parameter
val characterSheetId = call.queryParameters.characterSheetId
val diminished = call.queryParameters["diminished"]?.toIntOrNull()
?: throw MissingParameterException(
name = "diminished",
errorCode = APIResponse.MISSING_DIMINISHED
)
?: throw MissingParameterException(name = "diminished")
// Update the character damage
characterService.updateDiminished(
characterSheetId = characterSheetId,
@ -34,19 +32,9 @@ fun Engine.putCharacterDiminished(): suspend RoutingContext.() -> Unit {
diminished = diminished,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,8 +1,9 @@
package com.pixelized.server.lwa.server.rest.character
import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.MissingParameterException
import com.pixelized.server.lwa.server.exception.MissingParameterException
import com.pixelized.server.lwa.utils.extentions.characterSheetId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent
import io.ktor.server.response.respond
@ -14,10 +15,7 @@ fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit {
// get the query parameter
val characterSheetId = call.queryParameters.characterSheetId
val fatigue = call.queryParameters["fatigue"]?.toIntOrNull()
?: throw MissingParameterException(
name = "fatigue",
errorCode = APIResponse.MISSING_FATIGUE
)
?: throw MissingParameterException(name = "fatigue")
// fetch the character sheet
val characterSheet = characterService.character(characterSheetId)
?: error("CharacterSheet with id:$characterSheetId not found.")
@ -38,19 +36,9 @@ fun Engine.putCharacterFatigue(): suspend RoutingContext.() -> Unit {
fatigue = fatigue,
)
)
} catch (exception: MissingParameterException) {
call.respond(
message = APIResponse.error(
status = exception.errorCode,
message = exception.message ?: "?",
)
)
} catch (exception: Exception) {
call.respond(
message = APIResponse.error(
status = APIResponse.GENERIC,
message = exception.message ?: "?",
)
call.exception(
exception = exception
)
}
}

View file

@ -1,41 +1,24 @@
package com.pixelized.server.lwa.utils.extentions
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.server.lwa.server.exception.MissingParameterException
import io.ktor.http.Parameters
val Parameters.characterSheetId
get() = "characterSheetId".let { param ->
this[param] ?: throw MissingParameterException(
name = param,
errorCode = APIResponse.MISSING_CHARACTER_SHEET_ID,
)
this[param] ?: throw MissingParameterException(name = param)
}
val Parameters.alterationId
get() = "alterationId".let { param ->
this[param] ?: throw MissingParameterException(
name = param,
errorCode = APIResponse.MISSING_ALTERATION_ID,
)
this[param] ?: throw MissingParameterException(name = param)
}
val Parameters.create
get() = "create".let { param ->
this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException(
name = param,
errorCode = APIResponse.MISSING_CREATE
)
this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException(name = param)
}
val Parameters.active
get() = "active".let { param ->
this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException(
name = param,
errorCode = APIResponse.MISSING_ACTIVE
)
this[param]?.toBooleanStrictOrNull() ?: throw MissingParameterException(name = param)
}
class MissingParameterException(
name: String,
val errorCode: Int,
) : Exception("Missing $name parameter.")

View file

@ -0,0 +1,40 @@
package com.pixelized.server.lwa.utils.extentions
import com.pixelized.server.lwa.server.exception.BusinessException
import com.pixelized.server.lwa.server.exception.MissingParameterException
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingCall
suspend inline fun <reified T : Exception> RoutingCall.exception(exception: T) {
when (exception) {
is MissingParameterException -> {
respond(
message = APIResponse.error(
status = APIResponse.BAD_REQUEST,
message = exception.message ?: "?",
code = APIResponse.ErrorCode.AlterationName,
)
)
}
is BusinessException -> {
respond(
message = APIResponse.error(
status = APIResponse.INTERNAL_ERROR,
message = exception.message ?: "?",
code = exception.code,
)
)
}
else -> respond(
message = APIResponse.error(
status = APIResponse.INTERNAL_ERROR,
message = exception.message ?: "?",
)
)
}
}