Skip to content

Commit

Permalink
refactor: reorganize Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 24, 2024
1 parent 1eb84f0 commit abca348
Show file tree
Hide file tree
Showing 38 changed files with 129 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.ImagePageVO
import io.sakurasou.controller.vo.ImageVO
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.exception.FileSizeException
import io.sakurasou.exception.controller.param.FileSizeException
import io.sakurasou.extension.pageRequest
import io.sakurasou.plugins.AuthorizationPlugin
import java.io.ByteArrayOutputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.controller.vo.StrategyPageVO
import io.sakurasou.controller.vo.StrategyVO
import io.sakurasou.exception.WrongParameterException
import io.sakurasou.extension.id
import io.sakurasou.extension.pageRequest
import io.sakurasou.extension.success
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sakurasou.exception.controller.access

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
* 2024/9/12 11:08
*/
class PrincipalNotFoundException : ServiceThrowable() {
override val code: Int
get() = 5001
override val message: String
get() = "Principal Not Found"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.access

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.param

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.param

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.param

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.status

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.status

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.controller.status

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.dao

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sakurasou.exception.service.group

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
* 2024/9/24 13:59
*/
class GroupNotFoundException : ServiceThrowable() {
override val code: Int
get() = 4004
override val message: String
get() = "Group Not Found"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sakurasou.exception.service.permission

import io.sakurasou.exception.ServiceThrowable

/**
* @author ShiinaKin
* 2024/9/18 17:40
*/
class PermissionNotFoundException: ServiceThrowable() {
override val code: Int
get() = 4004
override val message: String
get() = "Permission Not Found"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.sakurasou.exception
package io.sakurasou.exception.service.role

import io.sakurasou.exception.ServiceThrowable

/**
* @author ShiinaKin
* 2024/9/18 17:40
*/
class RoleNotExistException: ServiceThrowable() {
class RoleNotFoundException: ServiceThrowable() {
override val code: Int
get() = 4004
override val message: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.service.setting

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sakurasou.exception.service.strategy

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
* 2024/9/23 08:22
*/
class StrategyNotFoundException : ServiceThrowable() {
override val code: Int
get() = 4004
override val message: String
get() = "Strategy Not Found"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package io.sakurasou.exception
package io.sakurasou.exception.service.user

import io.sakurasou.exception.ServiceThrowable

/**
* @author Shiina Kin
Expand All @@ -8,5 +10,5 @@ class UserNotFoundException : ServiceThrowable() {
override val code: Int
get() = 404
override val message: String
get() = "User not found"
get() = "User Not Found"
}
6 changes: 2 additions & 4 deletions app/src/main/kotlin/io/sakurasou/extension/AuthExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package io.sakurasou.extension

import io.ktor.server.application.*
import io.ktor.util.*
import io.sakurasou.config.InstanceCenter
import io.sakurasou.exception.PrincipalNotExistException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.exception.controller.access.PrincipalNotFoundException
import kotlinx.serialization.Serializable

/**
Expand All @@ -15,5 +13,5 @@ import kotlinx.serialization.Serializable
data class Principal(val id: Long, val groupId: Long, val username: String, val roles: List<String>)

fun ApplicationCall.getPrincipal(): Principal {
return attributes.getOrNull(AttributeKey("principal")) ?: throw PrincipalNotExistException()
return attributes.getOrNull(AttributeKey("principal")) ?: throw PrincipalNotFoundException()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.sakurasou.config.InstanceCenter
import io.sakurasou.controller.request.PageRequest
import io.sakurasou.controller.vo.CommonResponse
import io.sakurasou.exception.ServiceThrowable
import io.sakurasou.exception.WrongParameterException
import io.sakurasou.exception.controller.param.WrongParameterException

/**
* @author Shiina Kin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.sakurasou.model.dao.common

import io.sakurasou.controller.request.PageRequest
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.exception.PagingParameterWrongException
import io.sakurasou.exception.controller.param.PagingParameterWrongException
import io.sakurasou.model.dao.group.Groups
import io.sakurasou.model.dao.strategy.Strategies
import org.jetbrains.exposed.sql.*
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/io/sakurasou/plugins/Authorization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import io.ktor.server.auth.*
import io.ktor.server.auth.jwt.*
import io.ktor.util.*
import io.sakurasou.config.InstanceCenter
import io.sakurasou.exception.PrincipalNotExistException
import io.sakurasou.exception.UnauthorizedAccessException
import io.sakurasou.exception.controller.access.PrincipalNotFoundException
import io.sakurasou.exception.controller.status.UnauthorizedAccessException
import io.sakurasou.extension.Principal

/**
Expand All @@ -19,7 +19,7 @@ val AuthorizationPlugin = createRouteScopedPlugin(
) {
pluginConfig.apply {
on(AuthenticationChecked) { call ->
val principal = call.principal<JWTPrincipal>() ?: throw PrincipalNotExistException()
val principal = call.principal<JWTPrincipal>() ?: throw PrincipalNotFoundException()
val id = principal.payload.getClaim("id").asLong()
val groupId = principal.payload.getClaim("groupId").asLong()
val username = principal.payload.getClaim("username").asString()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/io/sakurasou/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import io.sakurasou.config.InstanceCenter.strategyService
import io.sakurasou.config.InstanceCenter.userService
import io.sakurasou.controller.*
import io.sakurasou.exception.ServiceThrowable
import io.sakurasou.exception.SiteNotInitializationException
import io.sakurasou.exception.controller.status.SiteNotInitializationException
import io.sakurasou.extension.failure
import io.sakurasou.extension.isSiteNotInitialized
import org.jetbrains.exposed.sql.exposedLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package io.sakurasou.service.auth

import at.favre.lib.crypto.bcrypt.BCrypt
import io.sakurasou.controller.request.UserLoginRequest
import io.sakurasou.exception.UnauthorizedAccessException
import io.sakurasou.exception.UserNotFoundException
import io.sakurasou.exception.controller.status.UnauthorizedAccessException
import io.sakurasou.exception.service.user.UserNotFoundException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dao.user.UserDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.sakurasou.service.common
import at.favre.lib.crypto.bcrypt.BCrypt
import io.sakurasou.config.InstanceCenter
import io.sakurasou.controller.request.SiteInitRequest
import io.sakurasou.exception.SiteRepeatedInitializationException
import io.sakurasou.exception.controller.status.SiteRepeatedInitializationException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.dao.album.AlbumDao
import io.sakurasou.model.dao.user.UserDao
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.sakurasou.controller.request.PageRequest
import io.sakurasou.controller.vo.GroupPageVO
import io.sakurasou.controller.vo.GroupVO
import io.sakurasou.controller.vo.PageResult
import io.sakurasou.exception.GroupNotExistException
import io.sakurasou.exception.RoleNotExistException
import io.sakurasou.exception.service.group.GroupNotFoundException
import io.sakurasou.exception.service.role.RoleNotFoundException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.dao.group.GroupDao
import io.sakurasou.model.dao.relation.RelationDao
Expand Down Expand Up @@ -36,7 +36,7 @@ class GroupServiceImpl(
runCatching {
relationDao.batchInsertGroupToRoles(groupId, groupRoles)
}.onFailure {
throw RoleNotExistException()
throw RoleNotFoundException()
}
}
}
Expand All @@ -60,7 +60,7 @@ class GroupServiceImpl(
}

override suspend fun fetchGroup(id: Long): GroupVO {
val group = dbQuery { groupDao.findGroupById(id) } ?: throw GroupNotExistException()
val group = dbQuery { groupDao.findGroupById(id) } ?: throw GroupNotFoundException()
val roles = dbQuery { relationDao.listRoleByGroupId(group.id) }
return GroupVO(
id = group.id,
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/kotlin/io/sakurasou/service/role/RoleServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package io.sakurasou.service.role

import io.sakurasou.controller.vo.PermissionVO
import io.sakurasou.controller.vo.RoleVO
import io.sakurasou.exception.PermissionNotExistException
import io.sakurasou.exception.RoleNotExistException
import io.sakurasou.exception.service.permission.PermissionNotFoundException
import io.sakurasou.exception.service.role.RoleNotFoundException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.dao.permission.PermissionDao
import io.sakurasou.model.dao.relation.RelationDao
Expand All @@ -25,7 +25,7 @@ class RoleServiceImpl(
val permissionNames = relationDao.listPermissionByRole(role.name)
val permissionVOList = permissionNames.map { permissionName ->
val permission = permissionDao.findPermissionByName(permissionName)
?: throw PermissionNotExistException()
?: throw PermissionNotFoundException()
PermissionVO(
permission.name,
permission.description
Expand All @@ -43,11 +43,11 @@ class RoleServiceImpl(
override suspend fun listRolesWithPermissionsOfUser(roleNames: List<String>): Map<String, RoleVO> {
return dbQuery {
roleNames.associate { roleName ->
val role = roleDao.findRoleByName(roleName) ?: throw RoleNotExistException()
val role = roleDao.findRoleByName(roleName) ?: throw RoleNotFoundException()
val permissionNames = relationDao.listPermissionByRole(roleName)
val permissionVOList = permissionNames.map { permissionName ->
val permission = permissionDao.findPermissionByName(permissionName)
?: throw PermissionNotExistException()
?: throw PermissionNotFoundException()
PermissionVO(
permission.name,
permission.description
Expand Down
Loading

0 comments on commit abca348

Please sign in to comment.