Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
package life.mosu.mosuserver.infra.notify.dto.discord;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public record DiscordExceptionNotifyEventRequest(
String exceptionCause,
String exceptionMessage,
String meta
String meta,
LocalDateTime timestamp
) {

public static DiscordExceptionNotifyEventRequest of(
String exceptionCause,
String exceptionMessage,
String meta
) {
return new DiscordExceptionNotifyEventRequest(exceptionCause, exceptionMessage, meta);
return new DiscordExceptionNotifyEventRequest(exceptionCause, exceptionMessage, meta,
LocalDateTime.now());
}

public static DiscordExceptionNotifyEventRequest of(
String exceptionCause,
String exceptionMessage
) {
return new DiscordExceptionNotifyEventRequest(exceptionCause, exceptionMessage, null);
return new DiscordExceptionNotifyEventRequest(exceptionCause, exceptionMessage, null,
LocalDateTime.now());
}

public String getMessage() {
return "⚠️ **알림 전송**\n"
+ String.format("- ⚠️ exception Cause : `%s`\n", exceptionCause)
+ String.format("- 📨 exception Message: `%s`\n", exceptionMessage)
+ String.format("- 📋 meta: `%s`", meta == null ? "없음" : meta);
+ String.format("- 📋 meta: `%s`\n", meta == null ? "없음" : meta)
+ String.format("- ⏰ timestamp: `%s`", timestamp.format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
Comment on lines 30 to 37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This method can be improved for both performance and readability.

  1. Performance: DateTimeFormatter.ofPattern(...) creates a new formatter instance on every call, which is inefficient. Since DateTimeFormatter is thread-safe, it's best to define it as a private static final constant in the record and reuse it.

  2. Readability: The string construction using + and multiple String.format calls can be simplified. Using a text block (available since Java 15) with the .formatted() method would make the code much cleaner.

Here is an example of how you could refactor this:

// Add this field to the record body
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// Replace the getMessage() method with this
public String getMessage() {
    return """
        ⚠️ **알림 전송**
        - ⚠️ exception Cause : `%s`
        - 📨 exception Message: `%s`
        - 📋 meta: `%s`
        - ⏰ timestamp: `%s`""".formatted(
            exceptionCause,
            exceptionMessage,
            meta == null ? "없음" : meta,
            timestamp.format(DATE_TIME_FORMATTER)
        );
}

}