我正在做一个国际象棋游戏,我在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-
package com.chess.exceptions;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class CustomExceptionsHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = InvalidMoveException.class)
@ResponseBody
protected ResponseEntity<Object> resolveInvalidMove(InvalidMoveException e, WebRequest req){
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.EXPECTATION_FAILED.value(),
HttpStatus.EXPECTATION_FAILED.getReasonPhrase(),
e.getMessage(),
req.getDescription(true));
return handleExceptionInternal(e, errorResponse.toString(), new HttpHeaders(), HttpStatus.EXPECTATION_FAILED, req);
}
下面是我两次修改resolvevalidmove方法以返回json的尝试-
ATTEMPT 1
@ExceptionHandler(value = InvalidMoveException.class)
@ResponseBody
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
public ErrorResponse resolveInvalidMove(InvalidMoveException e){
return new ErrorResponse(417, e.getMessage());
}
ATTEMPT 2
@ExceptionHandler(value = InvalidMoveException.class)
@ResponseBody
public ErrorResponse resolveInvalidMove(InvalidMoveException e, WebRequest req){
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.EXPECTATION_FAILED.value(),
HttpStatus.EXPECTATION_FAILED.getReasonPhrase(),
e.getMessage(),
req.getDescription(true));
return errorResponse;
错误响应.java
package com.chess.exceptions;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "error")
public class ErrorResponse {
private int status;
private String errReason;
private String errMessage;
private String path;
public ErrorResponse(int status, String errReason, String errMessage, String path) {
this.status = status;
this.errReason = errReason;
this.errMessage = errMessage;
this.path = path;
}
//2nd constructor added for ATTEMPT 1
public ErrorResponse(int status, String errMessage){
this.status = status;
this.errMessage = errMessage;
}
@Override
public String toString(){
return "Status Code: " + status + " " + errReason + " Message: " + errMessage + " at " + path;
}
}
下面是react-board.js的相关代码
DataService.makeMove(move)
.then(res => {
//console.log(res.data);
setIsWhite((prev) => !prev);
props.setTheBoard(res.data);
setStatus(res.data[64]);
updateMovesList();
})
.catch(err => {
console.log(err)
console.log(err.message)
})
数据服务.js
makeMove(move){
return axios.post(`${url}`, move);
}
任何帮助都将不胜感激
暂无答案!
目前还没有任何答案,快来回答吧!