@@ -173,31 +173,27 @@ public List<AuthenticateChallenge> parse() {
173173 scheme = token .scheme ;
174174 } else if (token .token68 != null ) {
175175 if (scheme == null ) {
176- throw LOGGER .atError ()
177- .addKeyValue ("challenge" , challenge )
178- .log (new IllegalArgumentException ("Challenge had token68 before scheme." ));
176+ String message = String .format ("Challenge '%s' had token68 before scheme." , challenge );
177+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
179178 } else if (token68 != null ) {
180- throw LOGGER .atError ()
181- .addKeyValue ("challenge" , challenge )
182- .log (new IllegalArgumentException ("Challenge had multiple token68s." ));
179+ String message = String .format ("Challenge '%s' had multiple token68s." , challenge );
180+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
183181 }
184182
185183 token68 = token .token68 ;
186184 } else if (token .authParam != null ) {
187185 if (scheme == null ) {
188- throw LOGGER .atError ()
189- .addKeyValue ("challenge" , challenge )
190- .log (new IllegalArgumentException ("Challenge had auth-param before scheme." ));
186+ String message = String .format ("Challenge '%s' had auth-param before scheme." , challenge );
187+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
191188 }
192189
193190 if (parameters == null ) {
194191 parameters = new LinkedHashMap <>();
195192 }
196193
197194 if (parameters .put (token .authParam .getKey (), token .authParam .getValue ()) != null ) {
198- throw LOGGER .atError ()
199- .addKeyValue ("challenge" , challenge )
200- .log (new IllegalArgumentException ("Challenge had duplicate auth-param." ));
195+ String message = String .format ("Challenge '%s' had duplicate auth-param." , challenge );
196+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
201197 }
202198 }
203199 }
@@ -218,9 +214,8 @@ private AuthenticateChallenge createChallenge(String scheme, String token68, Map
218214 return new AuthenticateChallenge (scheme , token68 );
219215 }
220216
221- throw LOGGER .atError ()
222- .addKeyValue ("challenge" , challenge )
223- .log (new IllegalArgumentException ("Challenge had both token68 and auth-params." ));
217+ String message = String .format ("Challenge '%s' had both token68 and auth-params." , challenge );
218+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
224219 }
225220
226221 boolean next () {
@@ -275,10 +270,9 @@ private AuthenticateChallengeToken handleSchemeToken(int schemeStartInclusive, i
275270 char currentChar , boolean alreadyInNextState ) {
276271 String scheme = challenge .substring (schemeStartInclusive , schemeEndExclusive );
277272 if (!isValidToken (scheme )) {
278- throw LOGGER .atError ()
279- .addKeyValue ("challenge" , challenge )
280- .addKeyValue ("scheme" , scheme )
281- .log (new IllegalArgumentException ("Scheme contained an invalid character." ));
273+ String message
274+ = String .format ("Scheme '%s' in challenge '%s' contained an invalid character." , scheme , challenge );
275+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
282276 }
283277
284278 // Iterate until the next non-space character, unless the scheme terminated with a comma.
@@ -317,10 +311,9 @@ private void handleScheme() {
317311 c = iterateUntilNextNonSpace ();
318312 if (c != '=' && c != ',' && currentIndex < challengeLength ) {
319313 // The next character is neither a comma nor an equal sign, throw an exception.
320- throw LOGGER .atError ()
321- .addKeyValue ("challenge" , challenge )
322- .log (new IllegalArgumentException (
323- "Challenge had more than one token68 or auth-param in the same comma separator." ));
314+ String message = String .format (
315+ "Challenge '%s' had more than one token68 or auth-param in the same comma separator." , challenge );
316+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
324317 }
325318
326319 if (c == ',' || currentIndex == challengeLength ) {
@@ -355,10 +348,9 @@ private void handleScheme() {
355348 // If the character following the last equal sign isn't a comma or end of challenge, there is an error.
356349 c = (c == ',' || currentIndex == challengeLength ) ? c : iterateUntilNextNonSpace ();
357350 if (currentIndex < challengeLength && c != ',' ) {
358- throw LOGGER .atError ()
359- .addKeyValue ("challenge" , challenge )
360- .log (new IllegalArgumentException (
361- "Challenge had more than one token68 or auth-param in the same comma separator." ));
351+ String message = String .format (
352+ "Challenge '%s' had more than one token68 or auth-param in the same comma separator." , challenge );
353+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
362354 }
363355 }
364356
@@ -370,10 +362,9 @@ private void handleScheme() {
370362 private char createAuthParamToken (int keyStartInclusive , int keyEndExclusive , char currentChar ) {
371363 String authParamKey = challenge .substring (keyStartInclusive , keyEndExclusive );
372364 if (!isValidToken (authParamKey )) {
373- throw LOGGER .atError ()
374- .addKeyValue ("challenge" , challenge )
375- .addKeyValue ("authParamKey" , authParamKey )
376- .log (new IllegalArgumentException ("Auth-param key contained an invalid character." ));
365+ String message = String .format ("Auth-param key '%s' in challenge '%s' contained an invalid character." ,
366+ authParamKey , challenge );
367+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
377368 }
378369
379370 int start = currentIndex ;
@@ -388,9 +379,9 @@ private char createAuthParamToken(int keyStartInclusive, int keyEndExclusive, ch
388379 currentChar = iterateUntil (c1 -> c1 == '"' && challenge .charAt (currentIndex - 1 ) != '\\' );
389380 if (currentChar != '"' ) {
390381 // Only time this should happen is reaching the end of the challenge.
391- throw LOGGER . atError ()
392- .addKeyValue ( " challenge" , challenge )
393- . log (new IllegalArgumentException ("Quoted-string was not terminated with a double quote." ));
382+ String message = String
383+ .format ( "Quoted-string in challenge '%s' was not terminated with a double quote. " , challenge );
384+ throw LOGGER . logThrowableAsError (new IllegalArgumentException (message ));
394385 }
395386
396387 authParamValue = challenge .substring (start , currentIndex ).replace ("\\ \\ " , "" );
@@ -399,10 +390,10 @@ private char createAuthParamToken(int keyStartInclusive, int keyEndExclusive, ch
399390 currentChar = iterateUntil (c1 -> c1 == ' ' || c1 == '\t' || c1 == ',' );
400391 authParamValue = challenge .substring (start , currentIndex );
401392 if (!isValidToken (authParamValue )) {
402- throw LOGGER . atError ()
403- . addKeyValue ( " challenge" , challenge )
404- . addKeyValue ( " authParamValue" , authParamValue )
405- . log (new IllegalArgumentException ("Auth-param value contained an invalid character." ));
393+ String message
394+ = String . format ( "Auth-param value '%s' in challenge '%s' contained an invalid character." ,
395+ authParamValue , challenge );
396+ throw LOGGER . logThrowableAsError (new IllegalArgumentException (message ));
406397 }
407398 }
408399
@@ -412,10 +403,9 @@ private char createAuthParamToken(int keyStartInclusive, int keyEndExclusive, ch
412403 // After the scheme only a single token68 or auth-param is allowed. If after any trailing spaces the next
413404 // character isn't a comma throw an exception.
414405 if (currentIndex < challengeLength && currentChar != ',' ) {
415- throw LOGGER .atError ()
416- .addKeyValue ("challenge" , challenge )
417- .log (new IllegalArgumentException (
418- "Challenge had more than one token68 or auth-param in the same comma separator." ));
406+ String message = String .format (
407+ "Challenge '%s' had more than one token68 or auth-param in the same comma separator." , challenge );
408+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
419409 }
420410
421411 token = new AuthenticateChallengeToken (null , null , new AbstractMap .SimpleEntry <>(authParamKey , authParamValue ));
@@ -490,21 +480,18 @@ private static String validateToken68(String challenge, int start, int end) {
490480 while (i < end ) {
491481 c = challenge .charAt (i );
492482 if (c != '=' ) {
493- throw LOGGER .atError ()
494- .addKeyValue ("challenge" , challenge )
495- .addKeyValue ("token68" , challenge .substring (start , end ))
496- .addKeyValue ("character" , c )
497- .log (new IllegalArgumentException ("Token68 contained invalid character." ));
483+ String message
484+ = String .format ("Token68 '%s' in challenge '%s' contained an invalid character '%s'." ,
485+ challenge .substring (start , end ), challenge , c );
486+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
498487 }
499488
500489 i ++;
501490 }
502491 } else if (!isValidToken68Character (c )) {
503- throw LOGGER .atError ()
504- .addKeyValue ("challenge" , challenge )
505- .addKeyValue ("token68" , challenge .substring (start , end ))
506- .addKeyValue ("character" , c )
507- .log (new IllegalArgumentException ("Token68 contained invalid character." ));
492+ String message = String .format ("Token68 '%s' in challenge '%s' contained an invalid character '%s'." ,
493+ challenge .substring (start , end ), challenge , c );
494+ throw LOGGER .logThrowableAsError (new IllegalArgumentException (message ));
508495 }
509496 }
510497
0 commit comments