简体   繁体   English

将SSE(Webflux)Spring Boot应用程序的错误消息传递给Angular 7前端

[英]Passing error messages from SSE (Webflux) Spring Boot app to Angular 7 frontend

My application is a Spring Boot app with several end points. 我的应用程序是一个Spring Boot应用程序,有几个端点。 We are trying to add an SSE enabled endpoint, using Webflux. 我们正在尝试使用Webflux添加支持SSE的端点。

Use case : 使用案例:

Step 1: Front-end submits a request to a POST endpoint and gets a unique ID. 步骤1:前端向POST端点提交请求并获取唯一ID。

Step 2: Front end fetches processed result using a GET endpoint (SSE enabled - Flux) 步骤2:前端使用GET端点获取处理结果(启用SSE - Flux)

Angular uses EventSource object to consume the SSE endpoint. Angular使用EventSource对象来使用SSE端点。 It requires the end point to produce text/event-stream. 它要求终点产生文本/事件流。 It works very well for positive test cases. 它对于正面测试用例非常有效。 But when runtime exceptions are thrown from the service, Angular frontend cannot get the HTTP status codes and exception details. 但是,当从服务抛出运行时异常时,Angular前端无法获取HTTP状态代码和异常详细信息。 It just errors with no data. 它只是没有数据的错误。

Thrown exceptions: 抛出异常:

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class RequestNotFoundException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public RequestNotFoundException(String message) {
        super(message);
    }
}

As mentioned in this , returning a Flux<ServerSentEvent> and terminating the flux with a custom event: 正如提到的 ,返回一个Flux<ServerSentEvent>和终止与一个自定义事件的通量:

Java endpoint Java端点

 @GetMapping(path= "{id}/async", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<Optional<ProcessedResult>>> getProcessedResultStream(
        @PathVariable("id") @ApiParam(value = "The unique identifier of the request", required = true) String id)
        throws InterruptedException {
    // Combining data and adding a disconnect event to handle at the frontend.
    return Flux.concat(
            Flux.just(ServerSentEvent.<Optional<ProcessedResult>>builder(service.getProcessedResult(id))
                    .build()),
            Flux.just(ServerSentEvent.<Optional<ProcessedResult>>builder().event("disconnect").build()));
}

This helps to close the connection at the UI end by disconnecting it. 这有助于通过断开连接来关闭UI端的连接。 But my main issue remains open. 但我的主要问题仍未解决。 How should I go about propagating error messages back to the frontend ? 我应该如何将错误消息传播回前端? All other (normal) endpoints throw custom runtime exceptions with proper HTTP codes as given above. 所有其他(普通)端点都会使用上面给出的正确HTTP代码抛出自定义运行时异常。

Should I change my ProcessedResult class to take exception details also ? 我是否应该更改我的ProcessedResult类以获取异常详细信息?

Server side events use an open HTTP connection to stream content from the server. 服务器端事件使用开放的HTTP连接来流式传输来自服务器的内容。 According to W3C spec of Eventsource HTTP 200 OK responses with a proper Content-Type header is required to establish the connection, HTTP 500, 502, 503, 504 or any other network errors will cause the browser to reestablish the connection. 根据事件源的W3C规范, 需要具有适当Content-Type头的HTTP 200 OK响应来建立连接, HTTP 500, 502, 503, 504 500,502,503,504或任何其他网络错误将导致浏览器重新建立连接。 If none of the above cases is satisfied an error event is triggered with readyState = 'CLOSED' . 如果上述情况均不满足,则使用readyState = 'CLOSED'触发错误事件。 In short, the onError callback is only for handling network timeouts or access control issues. 简而言之, onError回调仅用于处理网络超时或访问控制问题。

It is not possible to send a different HTTP status code based on runtime exception since you already send 200 Ok when you establish the connection. 由于您在建立连接时已经发送了200 Ok,因此无法根据运行时异常发送不同的HTTP状态代码。 The only way to handle runtime error is to catch the exception and send a custom named event from the server and close the EvetSource based on that. 处理运行时错误的唯一方法是捕获异常并从服务器发送自定义命名事件并基于此close EvetSource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导后端与 angular 前端 - Spring boot backend with angular frontend Spring Webflux SSE 服务器:如何向客户端发送错误并关闭连接 - Spring Webflux SSE server: how to send error to the client and close connection 在 spring 引导应用程序中启用 cors 时出错(反应前端) - Error in enabling cors in spring boot app (React Frontend) java spring 引导将数据传递到前端 - java spring boot passing data to frontend 尝试将 maven 依赖项从 spring-boot-starter-webflux 更新为 spring-webflux 时出现错误 UnsupportedMediaTypeException - Getting error UnsupportedMediaTypeException when trying to update maven dependency to spring-webflux from spring-boot-starter-webflux Spring Boot + Angular 4:自动重新编译前端 - Spring Boot + Angular 4 : Automatically recompile frontend Webflux Spring Boot应用程序的过滤器,该应用程序返回ResponseEntity <?> - Filters for webflux spring boot app, which return ResponseEntity<?> 使用Angular 7前端启动Spring-Boot应用程序时失败加载资源错误(缺少资源) - Failed to load resource error when starting Spring-Boot Application with Angular 7 Frontend (missing resources) 我从 Spring Boot 收到不充分的错误消息 - I'm Getting Inadequate Error Messages from Spring Boot 如何在 spring 引导中从前端查找实体? - How to find entity from frontend in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM