Skip to content

Commit

Permalink
Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
mnlipp committed Aug 23, 2023
1 parent 219d8aa commit c5818b6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void onConfigureQemu(RunnerConfigurationUpdate event) {
*/
@Handler
public void onHotpluggableCpuStatus(HotpluggableCpuStatus result) {
if (!result.successful()) {
logger.warning(() -> "Failed to get hotpluggable CPU status "
+ "(won't adjust number of CPUs.): " + result.errorMessage());
}

// Sort
List<ObjectNode> used = new ArrayList<>();
List<ObjectNode> unused = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.jdrupes.vmoperator.runner.qemu.events;

import com.fasterxml.jackson.databind.JsonNode;
import java.util.Optional;
import org.jdrupes.vmoperator.runner.qemu.commands.QmpAddCpu;
import org.jdrupes.vmoperator.runner.qemu.commands.QmpCommand;
import org.jdrupes.vmoperator.runner.qemu.commands.QmpDelCpu;
Expand Down Expand Up @@ -89,6 +90,7 @@ public boolean successful() {
*
* @return the json node
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public JsonNode values() {
if (response.has("return")) {
return response.get("return");
Expand All @@ -99,6 +101,43 @@ public JsonNode values() {
return null;
}

/**
* Returns the error class if this result is an error.
*
* @return the optional
*/
public Optional<String> errorClass() {
if (!response.has("error")) {
return Optional.empty();
}
return Optional.ofNullable(response.get("error").get("class").asText());
}

/**
* Returns the error description if this result is an error.
*
* @return the optional
*/
public Optional<String> errorDescription() {
if (!response.has("error")) {
return Optional.empty();
}
return Optional.ofNullable(response.get("error").get("desc").asText());
}

/**
* Combines error class and error description to a string
* "class: desc". Returns an empty string is this result is not an error.
*
* @return the string
*/
public String errorMessage() {
if (successful()) {
return "";
}
return errorClass().get() + ": " + errorDescription().get();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down

0 comments on commit c5818b6

Please sign in to comment.