001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.monitoring;
019
020import io.stallion.services.Log;
021import io.stallion.utils.json.JSON;
022import org.apache.commons.lang3.exception.ExceptionUtils;
023
024import java.lang.reflect.InvocationTargetException;
025import java.time.ZonedDateTime;
026import java.util.Collections;
027import java.util.Map;
028
029import static io.stallion.utils.Literals.*;
030import static io.stallion.Context.*;
031
032
033public class ExceptionInfo {
034    private String className;
035    private String message;
036    private ZonedDateTime thrownAt;
037    private String stackTrace;
038    private String requestUrl;
039    private String requestMethod;
040    private Map<String, String> requestHeaders;
041    private String requestBody = "";
042    private String remoteAddr;
043    private String actualIp;
044    private String outerMessage = "";
045    private String outerClassName = "";
046
047    public static ExceptionInfo newForException(Throwable e) {
048        ExceptionInfo info = new ExceptionInfo();
049        info.thrownAt = utcNow();
050        info.stackTrace = ExceptionUtils.getStackTrace(e);
051        if (e instanceof InvocationTargetException) {
052            e = ((InvocationTargetException)e).getTargetException();
053        }
054        info.className = e.getClass().getSimpleName();
055        info.message = e.getMessage();
056        info.requestUrl = request().requestUrl();
057        info.requestMethod = request().getMethod();
058        info.remoteAddr = request().getRemoteAddr();
059        info.actualIp = request().getActualIp();
060        info.requestHeaders = map();
061        for(String name: Collections.list(request().getHeaderNames())) {
062            info.requestHeaders.put(name, request().getHeader(name));
063        }
064        try {
065            info.setRequestBody(request().getContent());
066        } catch (RuntimeException e2) {
067            Log.info("Error logging the exception - could not get the request body: {0}", e2);
068        }
069        return info;
070    }
071
072    public String getRequestHeadersString() {
073        return JSON.stringify(requestHeaders);
074    }
075
076    public String getClassName() {
077        return className;
078    }
079
080    public void setClassName(String className) {
081        this.className = className;
082    }
083
084    public String getMessage() {
085        return message;
086    }
087
088    public void setMessage(String message) {
089        this.message = message;
090    }
091
092    public ZonedDateTime getThrownAt() {
093        return thrownAt;
094    }
095
096    public void setThrownAt(ZonedDateTime thrownAt) {
097        this.thrownAt = thrownAt;
098    }
099
100    public String getStackTrace() {
101        return stackTrace;
102    }
103
104    public void setStackTrace(String stackTrace) {
105        this.stackTrace = stackTrace;
106    }
107
108    public String getRequestUrl() {
109        return requestUrl;
110    }
111
112    public void setRequestUrl(String requestUrl) {
113        this.requestUrl = requestUrl;
114    }
115
116    public String getRequestMethod() {
117        return requestMethod;
118    }
119
120    public void setRequestMethod(String requestMethod) {
121        this.requestMethod = requestMethod;
122    }
123
124    public Map<String, String> getRequestHeaders() {
125        return requestHeaders;
126    }
127
128    public void setRequestHeaders(Map<String, String> requestHeaders) {
129        this.requestHeaders = requestHeaders;
130    }
131
132    public String getRequestBody() {
133        return requestBody;
134    }
135
136    public void setRequestBody(String requestBody) {
137        this.requestBody = requestBody;
138    }
139
140    public String getRemoteAddr() {
141        return remoteAddr;
142    }
143
144    public ExceptionInfo setRemoteAddr(String remoteAddr) {
145        this.remoteAddr = remoteAddr;
146        return this;
147    }
148
149    public String getActualIp() {
150        return actualIp;
151    }
152
153    public ExceptionInfo setActualIp(String actualIp) {
154        this.actualIp = actualIp;
155        return this;
156    }
157}