我正在尝试实现一个非常标准的任务:当发生异常时,重定向到我的/error页面.
在简化形式中,代码如下所示:
app.factory('$exceptionHandler', ['$location', function($location) {
return function(exception, cause) {
$location.path("/error");
};
}]);
Run Code Online (Sandbox Code Playgroud)
但是,AngularJS抱怨: 发现循环依赖:$ location < - $ exceptionHandler < - $ rootScope
这看起来像是一个基本限制,不允许$location在处理异常时使用.
但是我们还能怎么做呢?
我使用Spring的@ExceptionHandler注释来捕获控制器中的异常.
有些请求将POST数据保存为写入请求体的纯XML字符串,我想读取该数据以便记录异常.问题是,当我在异常处理程序中请求输入流并尝试从中读取时,流返回-1(空).
异常处理程序签名是:
@ExceptionHandler(Throwable.class)
public ModelAndView exception(HttpServletRequest request, HttpServletResponse response, HttpSession session, Throwable arff)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?有没有办法访问请求正文?
我的控制器:
@Controller
@RequestMapping("/user/**")
public class UserController {
static final Logger LOG = LoggerFactory.getLogger(UserController.class);
@Autowired
IUserService userService;
@RequestMapping("/user")
public ModelAndView getCurrent() {
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/firstLogin")
public ModelAndView firstLogin(HttpSession session) {
userService.logUser(session.getId());
userService.setOriginalAuthority();
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/login/failure")
public ModelAndView loginFailed() {
LOG.debug("loginFailed()");
Status status = new Status(-1,"Bad login");
return new ModelAndView("/user/login/failure", "response",status);
}
@RequestMapping("/user/login/unauthorized")
public ModelAndView unauthorized() {
LOG.debug("unauthorized()");
Status …Run Code Online (Sandbox Code Playgroud) 我的问题是我想创建一个@ExceptionHandler方法来捕获所有未处理的异常.一旦捕获,我想重定向到当前页面而不是指定一个单独的页面只是为了显示错误.
基本上我如何获取somemethod返回的someview的值,并在下面的方法unhandledExceptionHandler中动态设置它.
@ExceptionHandler(Exception.class)
protected ModelAndView unhandledExceptionHandler(Exception ex){
System.out.println("unhandle exception here!!!");
ModelAndView mv = new ModelAndView();
mv.setViewName("currentview");
mv.addObject("UNHANDLED_ERROR", "UNHANDLED ERROR. PLEASE CONTACT SUPPORT. "+ex.getMessage());
return mv;
}
@RequestMapping(value = "/somepage", method = RequestMethod.GET)
public String somemethod(HttpSession session) throws Exception {
String abc = null;
abc.length();
return "someview";
}
Run Code Online (Sandbox Code Playgroud)
所以在JSP中,我可以将此错误消息呈现回当前页面.
<c:if test="${not empty UNHANDLED_ERROR}">
<div class="messageError"> ${UNHANDLED_ERROR}</div>
</c:if>
Run Code Online (Sandbox Code Playgroud) 我有一个简单的Spring Rest Controller和一些验证.我的理解是验证失败会抛出MethodArgumentNotValidException.但是,我的代码抛出了BindException.在调试消息中,我还看到应用程序返回null ModelAndView.
为什么Rest Controller会抛出BindException或返回null ModelAndView?
注意:我正在使用curl测试我的Web应用程序并进行HTTP POST
curl -X POST http://localhost:8080/tasks
Run Code Online (Sandbox Code Playgroud)
我故意省略"name"参数,这是一个标有@NotNull和@NotBlank注释的必填字段.
我的控制器:
@RestController
public class TasksController {
private static Logger logger = Logger.getLogger(TasksController.class);
@Autowired
private MessageSource messageSource;
@Autowired
private Validator validator;
@InitBinder
protected void initBinder(WebDataBinder binder){
binder.setValidator(this.validator);
}
@RequestMapping(value = "/tasks", method = RequestMethod.POST)
public Task createTask(@Valid TasksCommand tasksCommand){
Task task = new Task();
task.setName(tasksCommand.getName());
task.setDue(tasksCommand.getDue());
task.setCategory(tasksCommand.getCategory());
return task;
}
}
Run Code Online (Sandbox Code Playgroud)
我的"命令"类(包含验证注释)
public class TasksCommand {
@NotBlank
@NotNull
private String name;
private Calendar due;
private String category; …Run Code Online (Sandbox Code Playgroud) 我有一个@ControllerAdvice类来处理来自 SpringMVC 控制器的异常。我想在方法中捕获已知类型 (RuntimeException)@ExceptionHandler的e.getCause()异常,然后抛出异常并让同一个 @ControllerAdvice 类捕获此异常。
示例代码:
@ControllerAdvice
public class ExceptionHandlingAdvice
{
@ExceptionHandler( RuntimeException.class )
private void handleRuntimeException( final RuntimeException e, final HttpServletResponse response ) throws Throwable
{
throw e.getCause(); // Can be of many types
}
// I want any Exception1 exception thrown by the above handler to be caught in this handler
@ExceptionHandler( Exception1.class )
private void handleAnException( final Exception1 e, final HttpServletResponse response ) throws Throwable
{
// handle exception
} …Run Code Online (Sandbox Code Playgroud) 在OmniFaces中,FullAjaxExceptionHandler在找到要使用的正确错误页面之后,调用JSF运行时来构建视图并呈现它而不是包含AJAX调用的页面.
为什么这个?恕我直言,只是执行一个ExternalContext#redirect()?有没有具体的理由这样做?
我们正在编写基于FullAjaxExceptionHandler的自己的ExceptionHandler,并希望了解这种设计背后的原因.
这很容易与异常助手中缺少的"查看详细信息"相同
但是我在VS 2017中看到它.翻转"使用托管兼容模式"似乎没有什么区别.
如果缺少"查看详细信息",复制详细信息也只会给出顶级异常.
如果我将代码包装在"try","catch"块中并使用调试器/立即窗口来扩展异常对象,我可以看到重要的细节,如"内部异常".那么为什么在处理程序中隐藏这些信息呢?
我正在开发一个Xamarin项目,这个错误位于一个名为.net核心可移植库的属性中,本例中的错误与安装在库中的NLog(第三方nuget包)有关. andorid项目,但由于配置错误可能会失败.在Android设备上进行远程调试时引发异常.
到目前为止,我已经看到了异常处理程序中的这个问题(3/4,它是一台新机器)
给这个控制器
@GetMapping("/test")
@ResponseBody
public String test() {
if (!false) {
throw new IllegalArgumentException();
}
return "blank";
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleException(Exception e) {
return "Exception handler";
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public String handleIllegalException(IllegalArgumentException e) {
return "IllegalArgumentException handler";
}
Run Code Online (Sandbox Code Playgroud)
这两个ExceptionHandler都匹配,IllegalArgumentException因为它是Exceptionclass 的子级。
当我到达/test端点时,将handleIllegalException调用该方法。如果抛出NullPointerException,handleException则调用该方法。
spring如何知道应该执行handleIllegalException方法而不是handleException方法?当多个ExceptionHandler匹配一个Exception 时,它如何管理优先级?
(我认为顺序或ExceptionHandler声明很重要,但是即使我handleIllegalException之前声明了handleException,结果也是一样的)
我想根据响应对象错误动态返回 HTTPStatus 代码,如 400、400、404 等。我被提到了这个问题 -使用 spring 3 restful以编程方式更改 http 响应状态,但它没有帮助。
我有一个带有@ExceptionHandler方法的控制器类
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
ExceptionUtility.getHttpCode(e.getCode()));
}
Run Code Online (Sandbox Code Playgroud)
ExceptionUtility是一个类,我在其中使用了上面使用的两种方法(getMessage和getCode)。
public class ExceptionUtility {
public static String getMessage(String message) {
return message;
}
public static HttpStatus getHttpCode(String code) {
return HttpStatus.NOT_FOUND; //how to return status code dynamically here ?
}
}
Run Code Online (Sandbox Code Playgroud)
我不想检查 if 条件并相应地返回响应代码,有没有其他更好的方法来做到这一点?
在spring web中,我们可以使用注释@ExceptionHandler来处理控制器的服务器和客户端错误.
我已经尝试使用这个注释与web-flux控制器,它仍然适用于我,但经过一些调查,我发现在这里
Spring Web Reactive的情况更复杂.由于反应流由与执行控制器方法的线程不同的线程评估,因此异常不会自动传播到控制器线程.这意味着@ExceptionHandler方法仅适用于直接处理请求的线程中引发的异常.如果我们想要使用@ExceptionHandler功能,则必须将流中抛出的异常传播回线程.这看起来有点令人失望,但在撰写本文时,Spring 5尚未发布,因此错误处理可能仍会变得更好.
所以我的问题是如何将异常传播回线程.有没有关于使用@ExceptionHandler和Spring web flux的好例子或文章?
更新:从spring.io看起来它支持,但仍然缺乏一般的理解
谢谢,
exceptionhandler ×10
java ×7
spring ×6
spring-mvc ×4
ajax ×1
angularjs ×1
dependencies ×1
jsf ×1
omnifaces ×1
redirect ×1
rest ×1