Reporting all build errors, not just first one

Hello,

Is there any solution to report all build errors, not just first one? Because now, I fix one error, start server, fix another one, start server (wait… maybe) fix third one…

How can we try to “compile” and generate list of all malformed files with errorneous line number, not just one?

Thanks

You cannot.

Yes… right now I am writing Java application which can analyze some of typical issues I encounter with tons documents I received…

Server just cannot start if one of documents malformed; otherwise maybe I can just “crawl” webpages to generate report (500 errors?) or create polite function “error handler” with Hugo… I am newbie :wink:

When Hugo encounters an error it cannot proceed.

You cannot “crawl” nor can you create a “polite” function.

Sorry.

Ok, I think I have some approach: write some application which will do this in a loop:

LOOP

  • start Hugo
  • catch build output into “build log”
  • if output has error & server cannot start, delete file (or mark it as “draft”?)
    GOTO start of LOOP

So that it will be automated; 10,000 files, 1000 malformed, and after few minutes run (with disabled taxonomy for faster run) I can probably get report of 1000 malformed files with suspicious line numbers & error descriptions… I will try it with Java since I know Java very well, but probably we can have such GO script.

Wow, it seems easy! I just need to improve this code a little, output errors in log file, delete erroneous file, start server again, in a loop:

package ca.ufg;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class CatchHugoErrors {

    public static final String HUGO_DIR = "/Users/fefendi/java/git/tokenizer-inc-canada/MyCoolWebsite/";
    public static void main(String[] args) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder("hugo", "server");
        pb.directory(new File(HUGO_DIR));
        Process p = pb.start();
        p.waitFor();  // wait for process to finish then continue.
        BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String output="";
        String line;
        while ((line = bri.readLine()) != null) {
            output+=line;
        }
        System.out.println(output);
    }
}

Ok, I was able to make it work in less than an hour, would be super cool if someone can create Node or Go command doing the same.

Naive code in Java, of course, just basic PoC, when it stops printing console logs… it means that old bad files were removed and logged, and server is up and running :wink:

package ca.ufg;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class CatchHugoErrors {
    public static final String HUGO_DIR = "/Users/fefendi/java/git/tokenizer-inc-canada/MyCoolWebsite/";

    public static void main(String[] args) throws IOException, InterruptedException {
        while (true) {
            try {
                String result = tryHugo();
                appendToFile("logs", "hugo-build-log.csv", result + "\n");
            } catch (Exception e) /* such as if no any error we will have weird parsing errors; naive code, sorry */ {
                break;
            }
        }
    }

    public static String tryHugo() throws Exception {
        ProcessBuilder pb = new ProcessBuilder("hugo", "server");
        pb.directory(new File(HUGO_DIR));
        Process p = pb.start();
        p.waitFor();  // wait for process to finish then continue.
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        String[] result;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            System.out.println("****************");
            if (line.startsWith("Error:")) {
                result = parseError(line);
                return result[0] + "\t" + result[1] + "\t" + result[2] + "\t" + result[3];
            }
        }
        return null;
    }

    static String ERROR_TYPE_1 = "Error: error building site: process: readAndProcessContent: \"";
    static String ERROR_TYPE_2 = "Error: error building site: \"";

    public static String[] parseError(String line) throws Exception {
        System.out.println(line);
        System.out.println("****************");
        if (line.startsWith(ERROR_TYPE_1)) {
            line = line.substring(ERROR_TYPE_1.length());
        } else if (line.startsWith(ERROR_TYPE_2)) {
            line = line.substring(ERROR_TYPE_2.length());
        }
        String[] res = new String[4];
        int index = line.indexOf(":");
        String file = line.substring(0, index);
        File file2
                = new File(file);
        if (file2.delete()) {
            System.out.println("File deleted successfully: " + file);
        }
        line = line.substring(index + 1);
        index = line.indexOf(":");
        String num1 = line.substring(0, index);
        line = line.substring(index + 1);
        index = line.indexOf("\":");
        String num2 = line.substring(0, index);
        line = line.substring(index + 1);
        line = line.substring(2);
        res[0] = file;
        res[1] = num1;
        res[2] = num2;
        res[3] = line;
        return res;
    }

    public static void appendToFile(String dir, String file, String content) throws IOException {
        String fullFilename = dir + File.separator + file;
        if (Files.exists(Paths.get(fullFilename))) {
            try (FileWriter fw = new FileWriter(fullFilename, true)) {
                fw.write(content);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            PrintWriter pw = new PrintWriter(new FileWriter(
                    fullFilename));
            pw.print(content);
            pw.close();
        }
    }
}