-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[YS-155] refact: 예외 핸들링 로직 수정 및 500 에러 디스코드 웹훅 알림 추가 (#37)
* feat: implement Discord error alarm notification feature * refact: delete unused file * chore: add discord env * chore: add discord env in test yml * refact: rename request dto name * style: delete unused import
- Loading branch information
Showing
10 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
src/main/kotlin/com/dobby/backend/domain/exception/DomainException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
package com.dobby.backend.domain.exception | ||
|
||
import org.springframework.http.HttpStatus | ||
|
||
open class DomainException( | ||
val errorCode: ErrorCode, | ||
val data: Any? = null, | ||
) : RuntimeException(errorCode.message) | ||
|
18 changes: 0 additions & 18 deletions
18
src/main/kotlin/com/dobby/backend/domain/exception/SignInRoleMismatchException.kt
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/dobby/backend/domain/gateway/AlertGateway.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.dobby.backend.domain.gateway | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
|
||
interface AlertGateway { | ||
fun sendError(e: Exception, request: HttpServletRequest) | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/dobby/backend/infrastructure/feign/discord/DiscordFeignClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.dobby.backend.infrastructure.feign.discord | ||
|
||
import com.dobby.backend.presentation.api.dto.request.DiscordMessageRequest | ||
import org.springframework.cloud.openfeign.FeignClient | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
|
||
@FeignClient( | ||
name = "discord-alarm-feign-client", | ||
url = "\${discord.webhook-url}" | ||
) | ||
interface DiscordFeignClient { | ||
|
||
@PostMapping(produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
fun sendMessage(@RequestBody discordMessageRequest: DiscordMessageRequest) | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/com/dobby/backend/infrastructure/gateway/discord/AlertGatewayImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.dobby.backend.infrastructure.gateway.discord | ||
|
||
import com.dobby.backend.domain.gateway.AlertGateway | ||
import com.dobby.backend.infrastructure.feign.discord.DiscordFeignClient | ||
import com.dobby.backend.presentation.api.dto.request.DiscordMessageRequest | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.springframework.stereotype.Component | ||
import java.io.PrintWriter | ||
import java.io.StringWriter | ||
import java.time.LocalDateTime | ||
|
||
@Component | ||
class AlertGatewayImpl( | ||
private val discordFeignClient: DiscordFeignClient | ||
): AlertGateway { | ||
|
||
override fun sendError(e: Exception, request: HttpServletRequest) { | ||
sendMessage(createMessage(e, request)) | ||
} | ||
|
||
private fun createMessage(e: Exception, request: HttpServletRequest): DiscordMessageRequest { | ||
return DiscordMessageRequest( | ||
content = "# 🚨 에러 발생 비이이이이사아아아앙", | ||
embeds = listOf( | ||
DiscordMessageRequest.Embed( | ||
title = "ℹ️ 에러 정보", | ||
description = """ | ||
### 🕖 발생 시간 | ||
${LocalDateTime.now()} | ||
### 🔗 요청 URL | ||
${createRequestFullPath(request)} | ||
### 📄 Stack Trace | ||
``` | ||
${getStackTrace(e).substring(0, 1000)} | ||
``` | ||
""".trimIndent() | ||
) | ||
) | ||
) | ||
} | ||
|
||
private fun createRequestFullPath(request: HttpServletRequest): String { | ||
var fullPath = "${request.method} ${request.requestURL}" | ||
|
||
val queryString = request.queryString | ||
if (queryString != null) { | ||
fullPath += "?$queryString" | ||
} | ||
|
||
return fullPath | ||
} | ||
|
||
private fun getStackTrace(e: Exception): String { | ||
val stringWriter = StringWriter() | ||
e.printStackTrace(PrintWriter(stringWriter)) | ||
return stringWriter.toString() | ||
} | ||
|
||
private fun sendMessage(request: DiscordMessageRequest) { | ||
discordFeignClient.sendMessage(request) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/com/dobby/backend/presentation/api/dto/request/DiscordMessageRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.dobby.backend.presentation.api.dto.request | ||
|
||
data class DiscordMessageRequest( | ||
val content: String? = null, | ||
val embeds: List<Embed>? = null | ||
) { | ||
|
||
data class Embed( | ||
val title: String? = null, | ||
val description: String? = null | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,6 @@ cloud: | |
credentials: | ||
access-key: test | ||
secret-key: test | ||
|
||
discord: | ||
webhook-url: test |