package org.rythmengine.spring.web;

import org.rythmengine.RythmEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by luog on 4/12/13.
 */
@ControllerAdvice
@EnableWebMvc
public class RythmExceptionHandler implements MessageSourceAware {

    private MessageSource messageSource;


   	public void setMessageSource(MessageSource messageSource) {
   		this.messageSource = messageSource;
   	}

    RythmEngine engine;

    @Autowired
    public RythmExceptionHandler(RythmConfigurer conf) {
        this.engine = conf.getRythmEngine();
    }

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(Exception e, HttpServletResponse response) throws Exception {
        if (engine.isProdMode() || e instanceof ServletException) {
            throw e;
        }
        ResponseStatus responseStatus = AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class);
        if (null != responseStatus) {
            int statusCode = responseStatus.value().value();
          		String reason = responseStatus.reason();
          		if (this.messageSource != null) {
          			reason = this.messageSource.getMessage(reason, null, reason, LocaleContextHolder.getLocale());
          		}
          		if (!StringUtils.hasLength(reason)) {
          			response.sendError(statusCode);
          		}
          		else {
          			response.sendError(statusCode, reason);
          		}
          		return new ModelAndView();
        }
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.setViewName("errors/500.html");
        return mav;
    }

    public static enum Util {
        ;

        public static List<StackTraceElement> tail(Throwable t) {
            ArrayList<StackTraceElement> l = new ArrayList<StackTraceElement>(Arrays.asList(t.getStackTrace()));
            if (l.size() > 0) l.remove(0);
            if (l.size() > 15) {
                for (int i = l.size() - 1; i >= 15; --i) {
                    l.remove(i);
                }
            }
            return l;
        }

        public static StackTraceElement head(Throwable t) {
            StackTraceElement[] sa = t.getStackTrace();
            if (sa.length == 0) {
                return null;
            }
            return sa[0];
        }
    }
}

@@ -331,34 +331,125 @@
 ore.
-annotation.AnnotationUtils
+Ordered;%0Aimport org.springframework.core.annotation.AnnotationUtils;%0Aimport org.springframework.core.annotation.Order
 ;%0Aim
@@ -1029,16 +1029,50 @@
 rAdvice%0A
+@Order(Ordered.LOWEST_PRECEDENCE)%0A
 @EnableW
@@ -1191,17 +1191,17 @@
 e;%0A%0A%0A   
-%09
+ 
 public v
@@ -1255,18 +1255,21 @@
 e) %7B%0A   
-%09%09
+     
 this.mes
@@ -1299,17 +1299,17 @@
 rce;%0A   
-%09
+ 
 %7D%0A%0A    R
@@ -1921,18 +1921,18 @@
         
-%09%09
+  
 String r
@@ -1970,26 +1970,26 @@
 ;%0A          
-%09%09
+  
 if (this.mes
@@ -2020,19 +2020,22 @@
         
+ 
-%09%09%09
+     
 reason =
@@ -2123,34 +2123,34 @@
 e());%0A          
-%09%09
+  
 %7D%0A          %09%09if
@@ -2145,18 +2145,18 @@
         
-%09%09
+  
 if (!Str
@@ -2187,35 +2187,38 @@
 n)) %7B%0A          
-%09%09%09
+      
 response.sendErr
@@ -2243,32 +2243,20 @@
         
-%09%09%7D%0A          %09%09
+  %7D 
 else %7B%0A 
@@ -2268,11 +2268,14 @@
     
-%09%09%09
+      
 resp
@@ -2320,18 +2320,18 @@
         
-%09%09
+  
 %7D%0A      
@@ -2338,10 +2338,10 @@
     
-%09%09
+  
 retu

format code