Project

General

Profile

1
package eu.dnetlib.openaire.exporter;
2

    
3
import org.apache.commons.lang3.StringUtils;
4
import org.apache.commons.lang3.exception.ExceptionUtils;
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.http.HttpStatus;
8
import org.springframework.web.bind.annotation.ExceptionHandler;
9
import org.springframework.web.bind.annotation.ResponseBody;
10
import org.springframework.web.bind.annotation.ResponseStatus;
11

    
12
/**
13
 * Created by claudio on 18/07/2017.
14
 */
15
public abstract class AbstractExporterController {
16

    
17
	private static final Log log = LogFactory.getLog(AbstractExporterController.class); // NOPMD by marko on 11/24/08 5:02 PM
18

    
19
	@ResponseBody
20
	@ExceptionHandler({Exception.class, Throwable.class})
21
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
22
	public ErrorMessage handleSqlException(final Exception e) {
23
		log.debug(e.getMessage(), e);
24
		if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) {
25
			return null;        //socket is closed, cannot return any response
26
		} else {
27
			return new ErrorMessage(e);
28
		}
29
	}
30

    
31
	public class ErrorMessage {
32

    
33
		private final String message;
34
		private final String stacktrace;
35

    
36
		public ErrorMessage(final Exception e) {
37
			this(e.getMessage(), ExceptionUtils.getStackTrace(e));
38
		}
39

    
40
		public ErrorMessage(final String message, final String stacktrace) {
41
			this.message = message;
42
			this.stacktrace = stacktrace;
43
		}
44

    
45
		public String getMessage() {
46
			return this.message;
47
		}
48

    
49
		public String getStacktrace() {
50
			return this.stacktrace;
51
		}
52
	}
53

    
54
}
    (1-1/1)