Source:
<code>public GatewayResponse authorise(CardAuthorisationGatewayRequest request) { logger.info("Calling Stripe for authorisation of charge [{}]", request.getChargeExternalId()); GatewayResponse.GatewayResponseBuilder<BaseResponse> responseBuilder = GatewayResponse .GatewayResponseBuilder .responseBuilder(); try { Response tokenResponse = createToken(request); Response sourceResponse = createSource( request, tokenResponse.readEntity(StripeTokenResponse.class).getId() ); StripeSourcesResponse stripeSourcesResponse = sourceResponse.readEntity(StripeSourcesResponse.class); if (stripeSourcesResponse.require3ds()) { Response source3dsResponse = create3dsSource(request, stripeSourcesResponse.getId()); return responseBuilder.withResponse(Stripe3dsSourceAuthorisationResponse.of(source3dsResponse)).build(); } else { Response authorisationResponse = createCharge(request, stripeSourcesResponse.getId()); StripeAuthorisationResponse stripeAuthResponse = new StripeAuthorisationResponse(authorisationResponse.readEntity(StripeCreateChargeResponse.class)); return responseBuilder.withResponse(stripeAuthResponse).build(); } } catch (GatewayClientException e) { Response response = e.getResponse(); int status = response.getStatus(); if (status == HttpStatus.SC_UNAUTHORIZED) { return unexpectedGatewayResponse(request.getChargeExternalId(), responseBuilder, e.getMessage()); } StripeErrorResponse stripeErrorResponse = response.readEntity(StripeErrorResponse.class); String errorId = UUID.randomUUID().toString(); logger.error("Authorisation failed for charge {}. Failure code from Stripe: {}, failure message from Stripe: {}. ErrorId: {}. Response code from Stripe: {}", request.getChargeExternalId(), stripeErrorResponse.getError().getCode(), stripeErrorResponse.getError().getMessage(), errorId, response.getStatus()); <START> GatewayError gatewayError = new GatewayError(stripeErrorResponse.getError().getMessage(), GENERIC_GATEWAY_ERROR); <END> return responseBuilder.withGatewayError(gatewayError).build(); } catch (DownstreamException e) { return unexpectedGatewayResponse(request.getChargeExternalId(), responseBuilder, e.getMessage()); } catch (GatewayException e) { return responseBuilder.withGatewayError(GatewayError.of(e)).build(); } }</code><technical_language>This works, static factory methods for specific cases. GatewayError.baseError(msg)</technical_language>

Target:
public GatewayResponse authorise(CardAuthorisationGatewayRequest request) { logger.info("Calling Stripe for authorisation of charge [{}]", request.getChargeExternalId()); GatewayResponse.GatewayResponseBuilder<BaseResponse> responseBuilder = GatewayResponse .GatewayResponseBuilder .responseBuilder(); try { Response tokenResponse = createToken(request); Response sourceResponse = createSource( request, tokenResponse.readEntity(StripeTokenResponse.class).getId() ); StripeSourcesResponse stripeSourcesResponse = sourceResponse.readEntity(StripeSourcesResponse.class); if (stripeSourcesResponse.require3ds()) { Response source3dsResponse = create3dsSource(request, stripeSourcesResponse.getId()); return responseBuilder.withResponse(Stripe3dsSourceAuthorisationResponse.of(source3dsResponse)).build(); } else { Response authorisationResponse = createCharge(request, stripeSourcesResponse.getId()); StripeAuthorisationResponse stripeAuthResponse = new StripeAuthorisationResponse(authorisationResponse.readEntity(StripeCreateChargeResponse.class)); return responseBuilder.withResponse(stripeAuthResponse).build(); } } catch (GatewayClientException e) { Response response = e.getResponse(); int status = response.getStatus(); if (status == HttpStatus.SC_UNAUTHORIZED) { return unexpectedGatewayResponse(request.getChargeExternalId(), responseBuilder, e.getMessage(), HttpStatus.SC_UNAUTHORIZED); } StripeErrorResponse stripeErrorResponse = response.readEntity(StripeErrorResponse.class); String errorId = UUID.randomUUID().toString(); logger.error("Authorisation failed for charge {}. Failure code from Stripe: {}, failure message from Stripe: {}. ErrorId: {}. Response code from Stripe: {}", request.getChargeExternalId(), stripeErrorResponse.getError().getCode(), stripeErrorResponse.getError().getMessage(), errorId, response.getStatus()); GatewayError gatewayError = genericGatewayError(stripeErrorResponse.getError().getMessage()); return responseBuilder.withGatewayError(gatewayError).build(); } catch (DownstreamException e) { return unexpectedGatewayResponse(request.getChargeExternalId(), responseBuilder, e.getMessage(), e.getStatusCode()); } catch (GatewayException e) { return responseBuilder.withGatewayError(GatewayError.of(e)).build(); } }