22
33import com.github.fastmirrorserver.dto.ApiResponse
44import com.github.fastmirrorserver.exception.ApiException
5+ import com.github.fastmirrorserver.service.ErrorReportService
56import com.github.fastmirrorserver.util.UTC
67import com.github.fastmirrorserver.util.uuid
78import org.slf4j.LoggerFactory
9+ import org.springframework.beans.factory.annotation.Autowired
810import org.springframework.web.bind.annotation.ExceptionHandler
911import org.springframework.web.bind.annotation.RestControllerAdvice
1012import org.springframework.web.util.NestedServletException
@@ -18,6 +20,9 @@ import javax.servlet.http.HttpServletResponse
1820@RestControllerAdvice
1921class ApiExceptionHandler {
2022 private val logger = LoggerFactory .getLogger(this ::class .java)
23+ @Autowired
24+ private lateinit var service: ErrorReportService
25+
2126 @ExceptionHandler(ApiException ::class )
2227 fun serviceExceptionHandler (e : ApiException , request : HttpServletRequest , response : HttpServletResponse ): ApiResponse {
2328 response.status = e.status.value()
@@ -29,17 +34,9 @@ class ApiExceptionHandler {
2934 if (e.cause != null && e.cause is ApiException ) return serviceExceptionHandler(e.cause as ApiException , request, response)
3035 logger.error(" uncaught exception: " , e)
3136 response.status = 500
32- val id = uuid().lowercase()
33- val file = File (" ./error-reports/${id} " )
34- file.parentFile.mkdirs()
35- file.createNewFile()
36- if (file.exists()) file.bufferedWriter().use {
37- it.write(" [${LocalDateTime .now().UTC } ] ${e::class .java.canonicalName} : ${e.message} " )
38- it.newLine()
39- val writer = PrintWriter (it)
40- e.printStackTrace(writer)
41- writer.flush()
42- }
37+
38+ val id = service.set(e)
39+
4340 return ApiResponse (
4441 data = mapOf<String , String >(
4542 " name" to e::class .java.name,
0 commit comments