14
14
15
15
package com .obs .services .exception ;
16
16
17
- import java .util .Locale ;
18
17
import java .util .Map ;
19
18
import java .util .regex .Matcher ;
20
19
import java .util .regex .Pattern ;
@@ -44,6 +43,10 @@ public class ObsException extends RuntimeException {
44
43
45
44
private String errorIndicator = null ;
46
45
46
+ private String obsExceptionString = null ;
47
+
48
+ private boolean calledToSting = false ;
49
+
47
50
public ObsException (String message ) {
48
51
this (message , null , null );
49
52
}
@@ -65,32 +68,46 @@ public ObsException(String message, String xmlMessage, Throwable cause) {
65
68
66
69
@ Override
67
70
public String toString () {
68
- StringBuilder myString = new StringBuilder (super .toString ());
69
-
70
- if (responseCode != -1 ) {
71
- myString .append (" -- ResponseCode: " )
72
- .append (responseCode )
73
- .append (", ResponseStatus: " )
74
- .append (responseStatus );
71
+ if (obsExceptionString != null ) {
72
+ return obsExceptionString ;
75
73
}
76
- if (isParsedFromXmlMessage ()) {
77
- myString .append (", XML Error Message: " ).append (xmlMessage );
78
- } else if (errorRequestId != null ) {
79
- myString .append (", RequestId: " ).append (errorRequestId ).append (", HostId: " ).append (errorHostId );
80
- }
81
- // 遍历Map的entry,打印所有报错相关头域
82
- Map <String , String > headers = getResponseHeaders ();
83
- if (headers != null ) {
84
- for (Map .Entry <String , String > header : headers .entrySet ()) {
85
- if (header .getKey ().toLowerCase (Locale .ROOT ).contains ("error" )) {
86
- myString .append (", ErrorHeaderKey: " )
87
- .append (header .getKey ())
88
- .append (", ErrorHeaderValue: " )
89
- .append (header .getValue ());
74
+ try {
75
+ calledToSting = true ;
76
+ StringBuilder myString = new StringBuilder (super .toString ());
77
+
78
+ if (responseCode != -1 ) {
79
+ myString .append (" -- ResponseCode: " )
80
+ .append (responseCode )
81
+ .append (", ResponseStatus: " )
82
+ .append (responseStatus );
83
+ }
84
+ if (isParsedFromXmlMessage ()) {
85
+ myString .append (", XML Error Message: " ).append (xmlMessage );
86
+ } else if (errorRequestId != null ) {
87
+ myString .append (", RequestId: " ).append (errorRequestId ).append (", HostId: " ).append (errorHostId );
88
+ }
89
+ // 遍历Map的entry,打印所有报错相关头域
90
+ Map <String , String > headers = getResponseHeaders ();
91
+ if (headers != null ) {
92
+ myString .append ("headers{" );
93
+ for (Map .Entry <String , String > header : headers .entrySet ()) {
94
+ myString .append (header .getKey ())
95
+ .append (":" )
96
+ .append (header .getValue ())
97
+ .append ("," );
90
98
}
99
+ myString .append ("}" );
91
100
}
101
+ obsExceptionString = myString .toString ();
102
+ return obsExceptionString ;
103
+ } finally {
104
+ calledToSting = false ;
92
105
}
93
- return myString .toString ();
106
+ }
107
+
108
+ @ Override
109
+ public String getMessage () {
110
+ return calledToSting ? super .getMessage () : this .toString ();
94
111
}
95
112
96
113
private boolean isParsedFromXmlMessage () {
0 commit comments