3838import java .io .OutputStream ;
3939
4040import org .apache .commons .compress .utils .IOUtils ;
41- import org .apache .logging .log4j .core .util .NullOutputStream ;
4241
4342import com .fasterxml .jackson .databind .DeserializationFeature ;
4443import com .fasterxml .jackson .databind .ObjectMapper ;
4544
4645import processing .app .Base ;
4746import processing .app .Editor ;
4847import processing .app .EditorTab ;
49- import processing .app .helpers .ProcessUtils ;
5048
5149public class ClangFormat implements Runnable {
5250
@@ -75,6 +73,8 @@ public void run() {
7573 } catch (IOException | InterruptedException e ) {
7674 editor .statusError ("Auto format error: " + e .getMessage ());
7775 e .printStackTrace ();
76+ } catch (ClangException e ) {
77+ editor .statusError ("Auto format error: " + e .getMessage ());
7878 }
7979 }
8080
@@ -101,23 +101,27 @@ private Thread copyAndClose(InputStream input, OutputStream output) {
101101 }
102102
103103 FormatResult runClangFormatOn (String source , int cursorOffset )
104- throws IOException , InterruptedException {
104+ throws IOException , InterruptedException , ClangException {
105105 String cmd [] = new String [] { clangExecutable , "--cursor=" + cursorOffset };
106106
107107 Process process = ProcessUtils .exec (cmd );
108108 ByteArrayOutputStream clangOutput = new ByteArrayOutputStream ();
109+ ByteArrayOutputStream clangError = new ByteArrayOutputStream ();
109110 ByteArrayInputStream dataOut = new ByteArrayInputStream (source .getBytes ());
110111
111112 Thread in = copyAndClose (dataOut , process .getOutputStream ());
112- Thread err = copyAndClose (process .getErrorStream (),
113- NullOutputStream .getInstance ());
113+ Thread err = copyAndClose (process .getErrorStream (), clangError );
114114 Thread out = copyAndClose (process .getInputStream (), clangOutput );
115115
116- /* int r = */ process .waitFor ();
116+ int r = process .waitFor ();
117117 in .join ();
118118 out .join ();
119119 err .join ();
120120
121+ if (r != 0 ) {
122+ throw new ClangException (clangError .toString ());
123+ }
124+
121125 // clang-format will output first a JSON object with:
122126 // - the resulting cursor position and
123127 // - a flag teling if the conversion was successful
@@ -141,6 +145,12 @@ FormatResult runClangFormatOn(String source, int cursorOffset)
141145 }
142146}
143147
148+ class ClangException extends Exception {
149+ public ClangException (String string ) {
150+ super (string );
151+ }
152+ }
153+
144154class FormatResult {
145155 public String FormattedText ;
146156 public int Cursor ;
0 commit comments