private < R > TypeAdapter < Xor < SaltError , R > > errorAdapter ( TypeAdapter < R > innerAdapter ) { return new TypeAdapter < Xor < SaltError , R > > ( ) { @ Override public Xor < SaltError , R > read ( JsonReader in ) throws IOException { JsonElement json = TypeAdapters . JSON_ELEMENT . read ( in ) ; try { R value = innerAdapter . fromJsonTree ( json ) ; return Xor . right ( value ) ; } catch ( Throwable e ) { Optional < SaltError > saltError = extractErrorString ( json ) . flatMap ( SaltErrorUtils :: deriveError ) ; return Xor . left ( saltError . orElse ( new JsonParsingError ( json , e ) ) ) ; } } private Optional < String > extractErrorString ( JsonElement json ) { if ( json . isJsonPrimitive ( ) && json . getAsJsonPrimitive ( ) . isString ( ) ) { return Optional . of ( json . getAsJsonPrimitive ( ) . getAsString ( ) ) ; } return Optional . empty ( ) ; } @ Override public void write ( JsonWriter out , Xor < SaltError , R > xor ) throws IOException { throw new JsonParseException ( "Writing Xor is not supported" ) ; } } ; }
Creates a Xor adapter specifically for the case in which the left side is a SaltError . This is used to catch any Salt - side or JSON parsing errors .