-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sse: send ErrorResponse to client via "event: error" on exception
Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
- Loading branch information
Showing
18 changed files
with
97 additions
and
40 deletions.
There are no files selected for viewing
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
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
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
17 changes: 14 additions & 3 deletions
17
core-ng/src/main/java/core/framework/internal/web/service/ErrorResponse.java
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,17 +1,28 @@ | ||
package core.framework.internal.web.service; | ||
|
||
import core.framework.api.json.Property; | ||
import core.framework.log.ErrorCode; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
public class ErrorResponse { | ||
public final class ErrorResponse { | ||
public static ErrorResponse errorResponse(Throwable e, String actionId) { | ||
var response = new ErrorResponse(); | ||
response.id = actionId; | ||
response.message = e.getMessage(); | ||
if (e instanceof ErrorCode errorCode) { | ||
response.errorCode = errorCode.errorCode(); | ||
} else { | ||
response.errorCode = "INTERNAL_ERROR"; | ||
} | ||
return response; | ||
} | ||
|
||
@Property(name = "id") | ||
public String id; | ||
|
||
@Property(name = "errorCode") | ||
public String errorCode; | ||
|
||
@Property(name = "message") | ||
public String message; | ||
} |
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
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
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
28 changes: 28 additions & 0 deletions
28
core-ng/src/test/java/core/framework/internal/web/sse/ServerSentEventHandlerTest.java
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,28 @@ | ||
package core.framework.internal.web.sse; | ||
|
||
import core.framework.util.Strings; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ServerSentEventHandlerTest { | ||
private ServerSentEventHandler handler; | ||
|
||
@BeforeEach | ||
void createServerSentEventHandler() { | ||
handler = new ServerSentEventHandler(null, null, null); | ||
} | ||
|
||
@Test | ||
void errorResponse() { | ||
byte[] error = handler.errorResponse(Strings.bytes("{\"error_code\": \"NOT_FOUND\"}")); | ||
assertThat(error).asString().isEqualTo(""" | ||
retry: 86400000 | ||
event: error | ||
data: {"error_code": "NOT_FOUND"} | ||
"""); | ||
} | ||
} |
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