Project

General

Profile

1
package eu.dnetlib.openaire.exporter;
2

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

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

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

    
20
	/*
21
	Tags used to group the operations on the swagger UI
22
	 */
23
	protected final static String DS = "datasource";
24
	protected final static String API = "API";
25
	protected final static String R = "read";
26
	protected final static String W = "write";
27

    
28
	protected final static String DSPACE = "DSpace";
29
	protected final static String EPRINT = "EPrints";
30
	protected final static String TSV = "TSV";
31
	protected final static String STREAMING = "Streaming";
32

    
33
	@ResponseBody
34
	@ExceptionHandler({Exception.class, Throwable.class})
35
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
36
	public ErrorMessage handleSqlException(final Exception e) {
37
		log.debug(e.getMessage(), e);
38
		if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) {
39
			return null;        //socket is closed, cannot return any response
40
		} else {
41
			return new ErrorMessage(e);
42
		}
43
	}
44

    
45
	@JsonAutoDetect
46
	public class ErrorMessage {
47

    
48
		private final String message;
49
		private final String stacktrace;
50

    
51
		public ErrorMessage(final Exception e) {
52
			this(e.getMessage(), ExceptionUtils.getStackTrace(e));
53
		}
54

    
55
		public ErrorMessage(final String message, final String stacktrace) {
56
			this.message = message;
57
			this.stacktrace = stacktrace;
58
		}
59

    
60
		public String getMessage() {
61
			return this.message;
62
		}
63

    
64
		public String getStacktrace() {
65
			return this.stacktrace;
66
		}
67
	}
68

    
69
}
    (1-1/1)