Skip to content

Commit

Permalink
Capture stderr of dot process; give helpful message on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Nov 5, 2020
1 parent eedae6f commit 6306c73
Showing 1 changed file with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,52 @@ Try<Void> executeDot( final ThrowingConsumer<OutputStream, IOException> contentP
try {
LOG.info( "Running dot: {}", command );
process = Runtime.getRuntime().exec( command, null, workingDir );
} catch ( final IOException exception ) {
return Try.failure( exception );
}

final OutputStream processStdIn = process.getOutputStream();
final InputStream processStdOut = process.getInputStream();
final InputStream processStdErr = process.getErrorStream();
final OutputStream processStdIn = process.getOutputStream();
final InputStream processStdOut = process.getInputStream();
final InputStream processStdErr = process.getErrorStream();

try {
contentProvider.accept( processStdIn );
final String graphvisErrorOutput = IOUtils.toString( processStdErr, StandardCharsets.UTF_8 );
if ( !graphvisErrorOutput.isEmpty() ) {
LOG.debug( "Dot returned an error: {}", graphvisErrorOutput );
final byte[] graphvizStdout = IOUtils.toByteArray( processStdOut );
final String graphvizStderr = IOUtils.toString( processStdErr, StandardCharsets.UTF_8 );

if ( !graphvizStderr.isEmpty() ) {
LOG.debug( "Dot returned an error: {}", graphvizStderr );
return Try.failure( new RuntimeException( "An error occured while running dot. This is most likely "
+ "due to a bug in owl-cli." ) );
}
} catch ( final IOException exception ) {
return Try.failure( exception );
}

return postprocess( processStdOut, configuration ).flatMap( processedOutput ->
writeStreamToOutput( processedOutput, output ).flatMap( writingResult -> {
LOG.debug( "Writing to output {}", output );
try {
process.waitFor();
return Try.success( null );
} catch ( final InterruptedException exception ) {
return Try.failure( exception );
if ( configuration.format == Configuration.Format.PNG ) {
output.write( graphvizStdout );
output.flush();
if ( output != System.out ) {
output.close();
}
} ) );
}

private Try<InputStream> postprocess( final InputStream inputStream, final Configuration configuration ) {
if ( configuration.format == Configuration.Format.PNG ) {
return Try.success( inputStream );
}
return Try.success( null );
}

try {
return svgPostProcessors.stream().sequential().reduce( Function.identity(), Function::andThen )
.apply( Try.success( IOUtils.toString( inputStream, StandardCharsets.UTF_8 ) ) )
.map( string -> IOUtils.toInputStream( string, StandardCharsets.UTF_8 ) );
final String svgOutput = new String( graphvizStdout, StandardCharsets.UTF_8 );
return postprocess( svgOutput ).flatMap( processedOutput ->
writeStreamToOutput( processedOutput, output ).flatMap( writingResult -> {
LOG.debug( "Writing to output {}", output );
try {
process.waitFor();
return Try.success( null );
} catch ( final InterruptedException exception ) {
return Try.failure( exception );
}
} ) );
} catch ( final IOException exception ) {
return Try.failure( exception );
}
}

private Try<InputStream> postprocess( final String dotOutput ) {
return svgPostProcessors.stream().sequential().reduce( Function.identity(), Function::andThen )
.apply( Try.success( dotOutput ) )
.map( string -> IOUtils.toInputStream( string, StandardCharsets.UTF_8 ) );
}

/**
* Performs diagram generation for an input ontology. The result is either written to a given {@link OutputStream}
* or a given {@link Path}.
Expand Down

0 comments on commit 6306c73

Please sign in to comment.