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 = exception.code,
                )
            )
        }

        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 ?: "?",
            )
        )
    }
}