tmc_langs_java/
error.rs

1//! Java error type
2
3use std::{io, path::PathBuf};
4use thiserror::Error;
5use tmc_langs_framework::TmcError;
6use tmc_langs_util::{FileError, JsonError};
7
8#[derive(Error, Debug)]
9pub enum JavaError {
10    #[error("Path {0} was not valid UTF-8")]
11    InvalidUtf8Path(PathBuf),
12    #[error("No java.home found in Java properties")]
13    NoJavaHome,
14    #[error("Maven did not output any class path")]
15    NoMvnClassPath,
16    #[error("{0} did not contain a valid exercise")]
17    InvalidExercise(PathBuf),
18    #[error("Failed to write temporary .jar file {0}")]
19    JarWrite(PathBuf, #[source] io::Error),
20    #[error("Failed to create temporary directory at")]
21    TempDir(#[source] io::Error),
22    #[error("Failed to find home directory")]
23    HomeDir,
24    #[error("Failed to find cache directory")]
25    CacheDir,
26    #[error("Failed to compile")]
27    Compilation { stdout: String, stderr: String },
28    #[error("Error while executing Java code")]
29    Jvm { stdout: String, stderr: String },
30
31    #[error("J4rs error
32stdout: {}
33stderr: {}
34",
35        stdout.as_deref().unwrap_or("none"),
36        stderr.as_deref().unwrap_or("none")
37    )]
38    J4rs {
39        stdout: Option<String>,
40        stderr: Option<String>,
41        source: j4rs::errors::J4RsError,
42    },
43    #[error("J4rs panicked: {0}")]
44    J4rsPanic(String),
45
46    #[error(transparent)]
47    WalkDir(#[from] walkdir::Error),
48    #[error("JSON error")]
49    Json(#[from] JsonError),
50    #[error("File IO error")]
51    FileError(#[from] FileError),
52    #[error("Error")]
53    Tmc(#[from] TmcError),
54}
55
56impl JavaError {
57    pub fn j4rs(source: j4rs::errors::J4RsError) -> Self {
58        Self::J4rs {
59            stdout: None,
60            stderr: None,
61            source,
62        }
63    }
64}
65
66// conversion from plugin error to TmcError::Plugin
67impl From<JavaError> for TmcError {
68    fn from(err: JavaError) -> TmcError {
69        TmcError::Plugin(Box::new(err))
70    }
71}
72
73// conversion from plugin error to a tmc result
74impl<T> From<JavaError> for Result<T, TmcError> {
75    fn from(from: JavaError) -> Self {
76        Err(TmcError::Plugin(Box::new(from)))
77    }
78}