在react from spring中捕获异常时遇到问题

l2osamch  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(345)

我正在做一个国际象棋游戏,我在Spring建立,然后连接到React。我在spring中有一些定制的异常,它们在后端可以很好地工作,但是我不知道如何在前端捕获我的定制消息。
我可以使用 Postman 与程序,如果这是一个非法的举动,我会收到这个消息- Status Code: 417 Expectation Failed Message: That is not a legal move for a PAWN at uri=/game;client=127.0.0.1 然而,在react中,我得到的唯一消息(除了来自xhr.js:178的消息)是- Request failed with status code 417 我试图遵循这里的建议-如何在react.js中显示服务器(java)异常消息,以及这里-如何在spring boot中以json返回异常,因为我的响应不是json格式(尽管我认为@responsebody应该将其转换为json),但是在尝试遵循接受的答案之后,它只导致了一个错误,没有解决,相反,React说,它与状态代码500失败,所以我显然错过了一些东西。
这是我原来的customerexceptionshandler-

  1. package com.chess.exceptions;
  2. import org.springframework.http.HttpHeaders;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.web.bind.annotation.ControllerAdvice;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import org.springframework.web.bind.annotation.ResponseStatus;
  9. import org.springframework.web.context.request.WebRequest;
  10. import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
  11. @ControllerAdvice
  12. public class CustomExceptionsHandler extends ResponseEntityExceptionHandler {
  13. @ExceptionHandler(value = InvalidMoveException.class)
  14. @ResponseBody
  15. protected ResponseEntity<Object> resolveInvalidMove(InvalidMoveException e, WebRequest req){
  16. ErrorResponse errorResponse = new ErrorResponse(HttpStatus.EXPECTATION_FAILED.value(),
  17. HttpStatus.EXPECTATION_FAILED.getReasonPhrase(),
  18. e.getMessage(),
  19. req.getDescription(true));
  20. return handleExceptionInternal(e, errorResponse.toString(), new HttpHeaders(), HttpStatus.EXPECTATION_FAILED, req);
  21. }

下面是我两次修改resolvevalidmove方法以返回json的尝试-

  1. ATTEMPT 1
  2. @ExceptionHandler(value = InvalidMoveException.class)
  3. @ResponseBody
  4. @ResponseStatus(HttpStatus.EXPECTATION_FAILED)
  5. public ErrorResponse resolveInvalidMove(InvalidMoveException e){
  6. return new ErrorResponse(417, e.getMessage());
  7. }
  8. ATTEMPT 2
  9. @ExceptionHandler(value = InvalidMoveException.class)
  10. @ResponseBody
  11. public ErrorResponse resolveInvalidMove(InvalidMoveException e, WebRequest req){
  12. ErrorResponse errorResponse = new ErrorResponse(HttpStatus.EXPECTATION_FAILED.value(),
  13. HttpStatus.EXPECTATION_FAILED.getReasonPhrase(),
  14. e.getMessage(),
  15. req.getDescription(true));
  16. return errorResponse;

错误响应.java

  1. package com.chess.exceptions;
  2. import javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement(name = "error")
  4. public class ErrorResponse {
  5. private int status;
  6. private String errReason;
  7. private String errMessage;
  8. private String path;
  9. public ErrorResponse(int status, String errReason, String errMessage, String path) {
  10. this.status = status;
  11. this.errReason = errReason;
  12. this.errMessage = errMessage;
  13. this.path = path;
  14. }
  15. //2nd constructor added for ATTEMPT 1
  16. public ErrorResponse(int status, String errMessage){
  17. this.status = status;
  18. this.errMessage = errMessage;
  19. }
  20. @Override
  21. public String toString(){
  22. return "Status Code: " + status + " " + errReason + " Message: " + errMessage + " at " + path;
  23. }
  24. }

下面是react-board.js的相关代码

  1. DataService.makeMove(move)
  2. .then(res => {
  3. //console.log(res.data);
  4. setIsWhite((prev) => !prev);
  5. props.setTheBoard(res.data);
  6. setStatus(res.data[64]);
  7. updateMovesList();
  8. })
  9. .catch(err => {
  10. console.log(err)
  11. console.log(err.message)
  12. })

数据服务.js

  1. makeMove(move){
  2. return axios.post(`${url}`, move);
  3. }

任何帮助都将不胜感激

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题