fix(provider/cf): stop NPE on empty error response
spinnaker/spinnaker#3648
MODIFY
clouddriver-cloudfoundry/src/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/CloudFoundryApiException.java
clouddriver-cloudfoundry/src/main/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/CloudFoundryApiException.java
@@ -21,19 +21,26 @@ import lombok.Getter;
import javax.annotation.Nullable;
import java.util.Objects;
+import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Arrays.stream;
@Getter
public class CloudFoundryApiException extends RuntimeException {
+ private static final String UNKNOWN_ERROR = "Unknown Error";
+
@Nullable
private ErrorDescription.Code errorCode;
public CloudFoundryApiException(ErrorDescription errorCause) {
- super(getMessage(errorCause.getErrors().toArray(new String[0])));
+ super(Optional.ofNullable(errorCause)
+ .map(e -> getMessage(e.getErrors().toArray(new String[0])))
+ .orElse(UNKNOWN_ERROR));
+ if (errorCause != null) {
this.errorCode = errorCause.getCode();
}
+ }
public CloudFoundryApiException(Throwable t, String... errors) {
super(getMessage(errors), t);

ADD
clouddriver-cloudfoundry/src/test/java/com/netflix/spinnaker/clouddriver/cloudfoundry/client/CloudFoundryApiExceptionTest.java
+/*
+ * Copyright 2018 Pivotal, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.netflix.spinnaker.clouddriver.cloudfoundry.client;
+
+import com.netflix.spinnaker.clouddriver.cloudfoundry.client.model.ErrorDescription;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class CloudFoundryApiExceptionTest {
+ @Test
+ void constructorWithErrorDescription() {
+ CloudFoundryApiException e = new CloudFoundryApiException(
+ new ErrorDescription().setErrors(Arrays.asList(
+ new ErrorDescription.Detail().setDetail("Main Error"),
+ new ErrorDescription.Detail().setDetail("Foo"))));
+
+ assertThat(e.getMessage()).contains("Main Error and Foo");
+ }
+
+ @Test
+ void constructorHandlesNullErrorDescription() {
+ CloudFoundryApiException e = new CloudFoundryApiException((ErrorDescription) null);
+
+ assertThat(e.getMessage()).contains("Unknown Error");
+ }
+}

fix(provider/cf): stop NPE on empty error response (#3180)

spinnaker/spinnaker#3648

Co-Authored-By: Stu Pollock <spollock@pivotal.io>