Search in sources :

Example 1 with JsonException

use of elemental.json.JsonException in project flow by vaadin.

the class BundleConfigurationReader method getFragments.

/**
 * Get the fragments data from the bundle configuration specified: fragment
 * name and a set of file paths as strings to the resources that are to be
 * included in the final produced fragment file.
 *
 * @return the fragments defined in the configuration file
 *
 * @throws IllegalStateException
 *             if parsed json file does not contain valid fragment data
 */
public Map<String, Set<String>> getFragments() {
    Map<String, Set<String>> fragments = new HashMap<>();
    if (bundleConfigurationJson == null || !bundleConfigurationJson.hasKey("fragments")) {
        return fragments;
    }
    JsonArray fragmentsArray;
    try {
        fragmentsArray = bundleConfigurationJson.getArray("fragments");
    } catch (JsonException | ClassCastException e) {
        throw new IllegalStateException("The 'fragments' property of a given bundle configuration should be an array.", e);
    }
    for (int i = 0; i < fragmentsArray.length(); ++i) {
        JsonObject fragment;
        try {
            fragment = fragmentsArray.getObject(i);
        } catch (JsonException | ClassCastException e) {
            throw new IllegalStateException("The 'fragments' array of a given bundle configuration should contain fragment objects only.", e);
        }
        String fragmentName = extractFragmentName(fragment);
        fragments.put(fragmentName, extractFragmentFiles(fragment, fragmentName));
    }
    return fragments;
}
Also used : JsonArray(elemental.json.JsonArray) JsonException(elemental.json.JsonException) Set(java.util.Set) HashMap(java.util.HashMap) JsonObject(elemental.json.JsonObject)

Example 2 with JsonException

use of elemental.json.JsonException in project flow by vaadin.

the class NodeUpdaterTest method getJsonFileContent_incorrectPackageJsonContent_throwsExceptionWithFileName.

@Test
public void getJsonFileContent_incorrectPackageJsonContent_throwsExceptionWithFileName() throws IOException {
    File brokenPackageJsonFile = temporaryFolder.newFile("broken-package.json");
    FileUtils.writeStringToFile(brokenPackageJsonFile, "{ some broken json ", UTF_8);
    JsonException exception = Assert.assertThrows(JsonException.class, () -> NodeUpdater.getJsonFileContent(brokenPackageJsonFile));
    MatcherAssert.assertThat(exception.getMessage(), StringContains.containsString("Cannot parse package file "));
    MatcherAssert.assertThat(exception.getMessage(), StringContains.containsString("broken-package.json"));
}
Also used : JsonException(elemental.json.JsonException) File(java.io.File) Test(org.junit.Test)

Example 3 with JsonException

use of elemental.json.JsonException in project che by eclipse.

the class JsonRpcEntityValidatorTest method shouldThrowJsonRpcExceptionWhenParsingFails.

@Test(expected = JsonRpcException.class)
public void shouldThrowJsonRpcExceptionWhenParsingFails() throws Exception {
    when(jsonFactory.parse(anyString())).thenThrow(new JsonException(""));
    validator.validate("message");
    verify(jsonFactory).parse("message");
}
Also used : JsonException(elemental.json.JsonException) Test(org.junit.Test)

Example 4 with JsonException

use of elemental.json.JsonException in project che by eclipse.

the class AppStateManager method restoreState.

private void restoreState(JsonObject settings) {
    try {
        if (settings.hasKey(WORKSPACE)) {
            JsonObject workspace = settings.getObject(WORKSPACE);
            for (String key : workspace.keys()) {
                if (persistenceComponents.containsKey(key)) {
                    StateComponent component = persistenceComponents.get(key);
                    component.loadState(workspace.getObject(key));
                }
            }
        }
    } catch (JsonException e) {
        Log.error(getClass(), e);
    }
}
Also used : JsonException(elemental.json.JsonException) JsonObject(elemental.json.JsonObject) StateComponent(org.eclipse.che.ide.api.component.StateComponent)

Example 5 with JsonException

use of elemental.json.JsonException in project flow by vaadin.

the class BundleConfigurationReader method extractFragmentFiles.

private Set<String> extractFragmentFiles(JsonObject fragment, String fragmentName) {
    JsonArray fragmentFiles;
    try {
        fragmentFiles = fragment.getArray("files");
        if (fragmentFiles == null) {
            throw new IllegalStateException(String.format("Fragment with name '%s' has no `files` array field specified.", fragmentName));
        }
    } catch (JsonException | ClassCastException e) {
        throw new IllegalStateException(String.format("Fragment with name '%s' has no `files` array field specified.", fragmentName), e);
    }
    Set<String> files = Sets.newHashSetWithExpectedSize(fragmentFiles.length());
    for (int j = 0; j < fragmentFiles.length(); ++j) {
        try {
            files.add(fragmentFiles.getString(j));
        } catch (JsonException | ClassCastException e) {
            throw new IllegalStateException(String.format("The 'files' array of a fragment with name '%s' should only contain string file paths", fragmentName), e);
        }
    }
    if (files.isEmpty()) {
        throw new IllegalStateException(String.format("Fragment with name '%s' has no files specified, each fragment should have at least one file specified", fragmentName));
    }
    return files;
}
Also used : JsonArray(elemental.json.JsonArray) JsonException(elemental.json.JsonException)

Aggregations

JsonException (elemental.json.JsonException)7 JsonObject (elemental.json.JsonObject)3 JsonArray (elemental.json.JsonArray)2 Test (org.junit.Test)2 UI (com.vaadin.flow.component.UI)1 JavaScriptBootstrapUI (com.vaadin.flow.component.internal.JavaScriptBootstrapUI)1 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)1 ResynchronizationRequiredException (com.vaadin.flow.server.communication.ServerRpcHandler.ResynchronizationRequiredException)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 StateComponent (org.eclipse.che.ide.api.component.StateComponent)1