initial commit

This commit is contained in:
2025-08-12 10:57:45 +08:00
commit 9e7acc571d
19 changed files with 670 additions and 0 deletions

98
core/build.gradle.kts Normal file
View File

@@ -0,0 +1,98 @@
import java.util.Properties
plugins {
alias(libs.plugins.dokka)
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.maven.publish)
}
repositories {
mavenCentral()
}
dependencies {
implementation(libs.ktor.client)
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
val javadocJar by tasks.register<Jar>("dokkaJavadocJar") {
dependsOn(tasks.dokkaJavadoc)
from(tasks.dokkaJavadoc.flatMap { it.outputDirectory })
archiveClassifier.set("javadoc")
}
val sourcesJar by tasks.register<Jar>("sourcesJar") {
from(sourceSets.main.get().allSource)
archiveClassifier.set("sources")
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = rootProject.group.toString()
artifactId = "http-response"
version = rootProject.version.toString()
from(components["java"])
artifact(javadocJar)
artifact(sourcesJar)
}
}
repositories {
val secretFile = rootProject.file("secret.properties")
if (secretFile.exists()) {
val secret = Properties()
secretFile.inputStream().also { secret.load(it) }
val mavenUrl = secret["neuon.maven.url"] as? String
val mavenUsername = secret["neuon.maven.username"] as? String
val mavenPassword = secret["neuon.maven.password"] as? String
if (mavenUrl != null && mavenUsername != null && mavenPassword != null) {
maven {
requireNotNull(mavenUrl) {
"neuon.maven.url is missing from secret.properties"
}
requireNotNull(mavenUsername) {
"neuon.maven.username is missing from secret.properties"
}
requireNotNull(mavenPassword) {
"neuon.maven.password is missing from secret.properties"
}
name = "neuon"
url = uri(mavenUrl)
credentials(PasswordCredentials::class) {
username = mavenUsername
password = mavenPassword
}
}
} else {
val list = listOfNotNull(
"neuon.maven.url".takeIf { mavenUrl == null },
"neuon.maven.username".takeIf { mavenUsername == null },
"neuon.maven.password".takeIf { mavenPassword == null },
)
val message = buildString {
append("Missing Maven publication in secret.properties: ")
append(list.joinToString())
append(". Ignore this if you don't plan on publishing this library")
}
logger.error(message)
}
} else {
logger.error(
buildString {
append("Missing secret.properties. Ignore this if you don't plan on publishing this library")
},
)
}
}
}

View File

@@ -0,0 +1,12 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
/**
* A base exception class for standardized error responses. The server should handle
* and give a standardized error response when this class is thrown
*/
abstract class BaseResponseException(
val status: HttpStatusCode, val errorMessage: String,
cause: Throwable? = null,
) : Exception(cause)

View File

@@ -0,0 +1,7 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
class ConflictResponseException(message: String) : BaseResponseException(
status = HttpStatusCode.Companion.Conflict, errorMessage = message,
)

View File

@@ -0,0 +1,6 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
class ForbiddenResponseException(message: String) :
BaseResponseException(status = HttpStatusCode.Companion.Forbidden, errorMessage = message)

View File

@@ -0,0 +1,7 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
class NotFoundResponseException(message: String = "Resource not found") : BaseResponseException(
status = HttpStatusCode.Companion.NotFound, errorMessage = message,
)

View File

@@ -0,0 +1,9 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
/**
* Should only be used explicitly in route handler
*/
open class ResponseException(status: HttpStatusCode, message: String) :
BaseResponseException(status = status, errorMessage = message)

View File

@@ -0,0 +1,8 @@
package ai.neuon.utility.http.response
import io.ktor.http.HttpStatusCode
class UnauthorizedResponseException : BaseResponseException(
status = HttpStatusCode.Companion.Unauthorized,
errorMessage = "No valid JWT is provided",
)