Skip to content

Commit

Permalink
feat: improve the response code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliothmoon committed Jan 7, 2025
1 parent 83ceec1 commit dad0d52
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/biz/devloper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.util.*
* @return [Resp]
*/
fun validateToken(token: String?): Resp {
val tk = token.throwIfNullOrEmpty("Token不能为空")
val tk = token.throwIfNullOrEmpty("Token不能为空", 400)
val qr = DB.from(ApplicationToken)
.select(ApplicationToken.status, ApplicationToken.id)
.where {
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/exception/ex.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package exception

class ServiceException : RuntimeException {
constructor(message: String?) : super(message)
constructor() : super()
class ServiceException(message: String?, val code: Int = 200) : RuntimeException(message) {

override fun fillInStackTrace() = this
}
3 changes: 2 additions & 1 deletion src/main/kotlin/router/router.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ private fun errHandler(ctx: RoutingContext) {
val ex = ctx.failure()
val resp = ctx.response()
resp.putHeader("Content-Type", "application/json")
resp.setStatusCode(500)
if (ex is ServiceException) {
ctx.response().setStatusCode(ex.code)
ctx.end(Resp.fail(ex.message).toJson())
log.error("ServiceException {}", ex.message)
return
}
resp.setStatusCode(500)
log.error("unexpected error ", ex)
ctx.end(UEE)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/utils/util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fun Boolean.throwIf(msg: String) {
}
}

fun <T> T?.throwIfNullOrEmpty(msg: String): T {
fun <T> T?.throwIfNullOrEmpty(msg: String, code: Int = 200): T {
if (this == null || (this is String && this.isEmpty())) {
throw ServiceException(msg)
throw ServiceException(msg, code)
}
return this
}
Expand Down

0 comments on commit dad0d52

Please sign in to comment.