Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more specific exceptions for deleteDashboard and getDashboard #4

Merged
merged 1 commit into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/main/java/com/appnexus/grafana/client/GrafanaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.appnexus.grafana.client.models.GrafanaDashboard;
import com.appnexus.grafana.client.models.GrafanaMessage;
import com.appnexus.grafana.configuration.GrafanaConfiguration;
import com.appnexus.grafana.exceptions.GrafanaDashboardCouldNotDeleteException;
import com.appnexus.grafana.exceptions.GrafanaDashboardDoesNotExistException;
import com.appnexus.grafana.exceptions.GrafanaException;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -71,17 +73,20 @@ public String getHost() {
*
* @param dashboardName the name of the dashboard to search for.
* @return {@link GrafanaDashboard} with matching name.
* @throws GrafanaException if a dashboard with matching name does not exist.
* @throws GrafanaDashboardDoesNotExistException if a dashboard with matching name does not exist.
* @throws GrafanaException if Grafana returns an error when trying to retrieve the dashboard.
* @throws IOException if a problem occurred talking to the server.
*/
public GrafanaDashboard getDashboard(String dashboardName) throws GrafanaException, IOException {
public GrafanaDashboard getDashboard(String dashboardName)
throws GrafanaDashboardDoesNotExistException, GrafanaException, IOException {

Response<GrafanaDashboard> response = service.getDashboard(apiKey, dashboardName).execute();

if (response.isSuccessful()) {
return response.body();
} else if (response.code() == HTTP_NOT_FOUND) {
throw new GrafanaException("Dashboard " + dashboardName + " does not exist");
throw new GrafanaDashboardDoesNotExistException(
"Dashboard " + dashboardName + " does not exist");
} else {
throw GrafanaException.withErrorBody(response.errorBody());
}
Expand Down Expand Up @@ -126,21 +131,25 @@ public DashboardMeta updateDashboard(GrafanaDashboard dashboard)
*
* @param dashboardName the name of the dashboard to delete.
* @return The name of the deleted dashboard.
* @throws GrafanaException if the dashboard does not exist or Grafana returns an error when
* trying to delete the dashboard.
* @throws GrafanaDashboardDoesNotExistException if the dashboard does not exist
* @throws GrafanaDashboardCouldNotDeleteException if Grafana returns an error when trying to
* delete the dashboard.
* @throws IOException if a problem occurred talking to the server.
*/
public String deleteDashboard(String dashboardName) throws GrafanaException, IOException {
public String deleteDashboard(String dashboardName)
throws GrafanaDashboardDoesNotExistException, GrafanaDashboardCouldNotDeleteException,
IOException {

Response<DashboardSuccessfulDelete> response =
service.deleteDashboard(apiKey, dashboardName).execute();

if (response.isSuccessful()) {
return response.body().title();
} else if (response.code() == HTTP_NOT_FOUND) {
throw new GrafanaException("Dashboard " + dashboardName + " does not exist");
throw new GrafanaDashboardDoesNotExistException(
"Dashboard " + dashboardName + " does not exist");
} else {
throw GrafanaException.withErrorBody(response.errorBody());
throw GrafanaDashboardCouldNotDeleteException.withErrorBody(response.errorBody());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Licensed under Apache-2.0 */
package com.appnexus.grafana.exceptions;

import java.io.IOException;
import okhttp3.ResponseBody;

public class GrafanaDashboardCouldNotDeleteException extends GrafanaException {

public static GrafanaDashboardCouldNotDeleteException withErrorBody(ResponseBody body)
throws IOException {
return body != null
? new GrafanaDashboardCouldNotDeleteException("Unexpected Grafana error; " + body.string())
: new GrafanaDashboardCouldNotDeleteException("Unexpected Grafana error");
}

public GrafanaDashboardCouldNotDeleteException(String message) {
super(message);
}

public GrafanaDashboardCouldNotDeleteException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Licensed under Apache-2.0 */
package com.appnexus.grafana.exceptions;

import java.io.IOException;
import okhttp3.ResponseBody;

public class GrafanaDashboardDoesNotExistException extends GrafanaException {

public static GrafanaDashboardDoesNotExistException withErrorBody(ResponseBody body)
throws IOException {
return body != null
? new GrafanaDashboardDoesNotExistException("Unexpected Grafana error; " + body.string())
: new GrafanaDashboardDoesNotExistException("Unexpected Grafana error");
}

public GrafanaDashboardDoesNotExistException(String message) {
super(message);
}

public GrafanaDashboardDoesNotExistException(String message, Throwable cause) {
super(message, cause);
}
}