1- package com .relogiclabs .json . schema .exception ;
1+ package com .relogiclabs .jschema .exception ;
22
3- import com .relogiclabs .json . schema .message .ErrorDetail ;
3+ import com .relogiclabs .jschema .message .ErrorDetail ;
44import lombok .Getter ;
55
66import java .util .HashMap ;
77import java .util .Map ;
88
9+ import static java .util .Arrays .copyOfRange ;
10+
911public class CommonException extends RuntimeException {
12+ private static final String FAIL_METHOD_PREFIX = "fail" ;
1013 @ Getter private final String code ;
1114 private Map <String , String > attributes ;
1215
1316 public CommonException (String code , String message , Throwable cause ) {
1417 super (message , cause );
1518 this .code = code ;
19+ formatStackTrace ();
1620 }
1721
1822 public CommonException (String code , String message ) {
@@ -36,4 +40,20 @@ public void setAttribute(String name, String value) {
3640 if (attributes == null ) attributes = new HashMap <>(5 );
3741 attributes .put (name , value );
3842 }
43+
44+ @ Override
45+ public synchronized Throwable fillInStackTrace () {
46+ var result = super .fillInStackTrace ();
47+ formatStackTrace ();
48+ return result ;
49+ }
50+
51+ private void formatStackTrace () {
52+ StackTraceElement [] stackTrace = getStackTrace ();
53+ int offset = 0 ;
54+ for (var e : stackTrace )
55+ if (e .getMethodName ().startsWith (FAIL_METHOD_PREFIX )) offset ++;
56+ else break ;
57+ setStackTrace (copyOfRange (stackTrace , offset , stackTrace .length ));
58+ }
3959}
0 commit comments