-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling and logging (#237)
* Fix error handling and improve logging * Bump parent and deps versions
- Loading branch information
Showing
14 changed files
with
803 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/info/freelibrary/iiif/webrepl/EnvOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
package info.freelibrary.iiif.webrepl; | ||
|
||
import java.time.Duration; | ||
|
||
import org.microhttp.Options; | ||
|
||
import info.freelibrary.util.Constants; | ||
import info.freelibrary.util.Env; | ||
|
||
/** | ||
* The server's environmental options. | ||
*/ | ||
public class EnvOptions { | ||
|
||
/** The maximum request size. */ | ||
private static final int DEFAULT_MAX_REQ_SIZE = 1_024 * 1_024; | ||
|
||
/** The default port at which the server listens. */ | ||
private static final int DEFAULT_PORT = 8888; | ||
|
||
/** The read buffer size. */ | ||
private static final int DEFAULT_READ_BUF_SIZE = 1_024 * 64; | ||
|
||
/** The request timeout. */ | ||
private static final long DEFAULT_REQ_TIMEOUT = 60L; | ||
|
||
/** | ||
* Gets the environmental options. | ||
* | ||
* @return The configuration options | ||
*/ | ||
public Options getOpts() { | ||
final int port = Env.get(Config.HTTP_PORT, DEFAULT_PORT); | ||
final int reqSize = Env.get(Config.MAX_REQUEST_SIZE, DEFAULT_MAX_REQ_SIZE); | ||
final int bufSize = Env.get(Config.READ_BUFFER_SIZE, DEFAULT_READ_BUF_SIZE); | ||
final long timeout = (long) Env.get(Config.REQUEST_TIMEOUT, DEFAULT_REQ_TIMEOUT); | ||
|
||
return Options.builder().withHost(Constants.INADDR_ANY).withPort(port).withMaxRequestSize(reqSize) | ||
.withRequestTimeout(Duration.ofSeconds(timeout)).withReadBufferSize(bufSize).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
package info.freelibrary.iiif.webrepl; | ||
|
||
import static info.freelibrary.util.Constants.EOL; | ||
import static info.freelibrary.util.StringUtils.addLineNumbers; | ||
import static info.freelibrary.util.StringUtils.indent; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import info.freelibrary.util.warnings.PMD; | ||
import info.freelibrary.util.warnings.Sonar; | ||
|
||
/** | ||
* A string of imports. | ||
*/ | ||
public class Imports { | ||
|
||
/** A label for the display of imports in the logs. */ | ||
private static final String LABEL = "Imports:"; | ||
|
||
/** A string of imports. */ | ||
private final String myImports; | ||
|
||
/** | ||
* Creates a new imports string. | ||
* | ||
* @throws IOException If there is trouble reading from the imports file | ||
*/ | ||
public Imports() throws IOException { | ||
File importsFile = Path.of("/etc/jshell/imports.jsh").toFile(); | ||
|
||
// Check to see if we're running from the Maven build | ||
if (!importsFile.exists()) { | ||
importsFile = Path.of("src/main/docker/imports.jsh").toFile(); | ||
} | ||
|
||
try (BufferedReader reader = Files.newBufferedReader(importsFile.toPath())) { | ||
myImports = reader.lines().filter(line -> !line.isBlank()).collect(joining(EOL)); | ||
} | ||
} | ||
|
||
/** | ||
* Gets all the imports. | ||
* | ||
* @return An imports string | ||
*/ | ||
@SuppressWarnings({ Sonar.SYSTEM_OUT_ERR, PMD.SYSTEM_PRINTLN }) | ||
public String getAll() { | ||
if (myImports.isEmpty()) { | ||
System.err.println(); | ||
} else { | ||
System.err.println(EOL + LABEL); | ||
System.err.println(indent(addLineNumbers(myImports), 2)); | ||
} | ||
|
||
return myImports; | ||
} | ||
|
||
/** | ||
* Gets all the imports that are referenced in the supplied code snippet. | ||
* | ||
* @param aSnippet A code snippet to check for imports | ||
* @return An imports string | ||
* @throws IOException if there is trouble parsing the imports | ||
*/ | ||
@SuppressWarnings({ Sonar.SYSTEM_OUT_ERR, PMD.SYSTEM_PRINTLN }) | ||
public String getReferenced(final String aSnippet) throws IOException { | ||
final String imports; | ||
|
||
try (BufferedReader reader = new BufferedReader(new StringReader(myImports))) { | ||
imports = reader.lines().filter(line -> { | ||
final String className = line.substring(line.lastIndexOf('.') + 1, line.length() - 1); | ||
return aSnippet == null || aSnippet.contains(className); | ||
}).collect(joining(EOL)).trim(); | ||
} | ||
|
||
// The above gets imports for the user, the below shows the imported imports in the logs | ||
if (imports.isEmpty()) { | ||
System.err.println(); | ||
} else { | ||
System.err.println(EOL + LABEL); | ||
System.err.println(indent(addLineNumbers(imports), 2)); | ||
} | ||
|
||
return imports; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/info/freelibrary/iiif/webrepl/ParsingError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
package info.freelibrary.iiif.webrepl; | ||
|
||
import info.freelibrary.util.Constants; | ||
import info.freelibrary.util.StringUtils; | ||
|
||
/** | ||
* A {@code WebRepl} parsing error. | ||
*/ | ||
public class ParsingError extends Exception { | ||
|
||
/** The message template used in constructing the exception message. */ | ||
static final String MESSAGE_TEMPLATE = | ||
"Parsing error, but there wasn't a useful diagnostic message. Submitted source code:" + Constants.EOL + | ||
Constants.EOL + "{}"; | ||
|
||
/** The {@code serialVersionUID} for the ParsingError class. */ | ||
private static final long serialVersionUID = 8614571124201029070L; | ||
|
||
/** | ||
* Creates a parsing error for the supplied source. This type of parsing error lacks any diagnostic information from | ||
* JShell. | ||
* | ||
* @param aSource A source code snippet | ||
*/ | ||
public ParsingError(final String aSource) { | ||
super(StringUtils.format(MESSAGE_TEMPLATE, aSource)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getMessage(); | ||
} | ||
} |
Oops, something went wrong.