@@ -32,6 +32,7 @@ public final class ServiceException extends RuntimeException implements SafeLogg
3232
3333    private  final  String  errorId  = UUID .randomUUID ().toString ();
3434    private  final  String  safeMessage ;
35+     private  final  String  noArgsMessage ;
3536
3637    /** 
3738     * Creates a new exception for the given error. All {@link com.palantir.logsafe.SafeArg safe} parameters are 
@@ -48,12 +49,13 @@ public ServiceException(ErrorType errorType, @Nullable Throwable cause, Arg<?>..
4849
4950    private  ServiceException (ErrorType  errorType , @ Nullable  Throwable  cause , List <Arg <?>> args ) {
5051        // TODO(rfink): Memoize formatting? 
51-         super (safeMessage (errorType , args ), cause );
52+         super (renderSafeMessage (errorType , args ), cause );
5253
5354        this .errorType  = errorType ;
5455        // Note that instantiators cannot mutate List<> args since it comes through copyToList in all code paths. 
5556        this .args  = Collections .unmodifiableList (args );
56-         this .safeMessage  = safeMessage (errorType , args );
57+         this .safeMessage  = renderSafeMessage (errorType , args );
58+         this .noArgsMessage  = renderNoArgsMessage (errorType );
5759    }
5860
5961    /** The {@link ErrorType} that gave rise to this exception. */ 
@@ -68,18 +70,18 @@ public String getErrorId() {
6870
6971    @ Override 
7072    public  String  getMessage () {
73+         // Including safe args here since any logger not configured with safe-logging will log this message. 
7174        return  safeMessage ;
7275    }
7376
7477    @ Override 
7578    public  String  getLogMessage () {
76-         return  safeMessage ;
79+         // Not returning safe args here since the safe-logging framework will log this message + args explicitly. 
80+         return  noArgsMessage ;
7781    }
7882
79-     private  static  String  safeMessage (ErrorType  errorType , List <Arg <?>> args ) {
80-         String  message  = errorType .code ().name ().equals (errorType .name ())
81-                 ? String .format ("ServiceException: %s" , errorType .code ())
82-                 : String .format ("ServiceException: %s (%s)" , errorType .code (), errorType .name ());
83+     private  static  String  renderSafeMessage (ErrorType  errorType , List <Arg <?>> args ) {
84+         String  message  = renderNoArgsMessage (errorType );
8385
8486        if  (args .isEmpty ()) {
8587            return  message ;
@@ -102,6 +104,12 @@ private static String safeMessage(ErrorType errorType, List<Arg<?>> args) {
102104        return  builder .toString ();
103105    }
104106
107+     private  static  String  renderNoArgsMessage (ErrorType  errorType ) {
108+         return  errorType .code ().name ().equals (errorType .name ())
109+                 ? String .format ("ServiceException: %s" , errorType .code ())
110+                 : String .format ("ServiceException: %s (%s)" , errorType .code (), errorType .name ());
111+     }
112+ 
105113    @ Override 
106114    public  List <Arg <?>> getArgs () {
107115        return  args ;
0 commit comments