-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use miette for json parsing errors (#1318)
* use miette for everything, i think i should use miette just for json decode for now * only on ls-remote * fix test * use .json instead of failing all the time * fix test * changeset * remove unnecessary feature * remove test for now * remove unnecessary .to_string()
- Loading branch information
Showing
10 changed files
with
202 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fnm": patch | ||
--- | ||
|
||
better error handling for malformed json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,51 @@ | ||
use miette::SourceOffset; | ||
|
||
#[derive(Debug, thiserror::Error, miette::Diagnostic)] | ||
#[error("malformed json\n{}", self.report())] | ||
pub struct DecodeError { | ||
cause: serde_json::Error, | ||
#[source_code] | ||
input: String, | ||
#[label("at this position")] | ||
location: SourceOffset, | ||
} | ||
|
||
#[derive(Debug, thiserror::Error, miette::Diagnostic)] | ||
#[error("")] | ||
pub struct ClonedError { | ||
message: String, | ||
#[source_code] | ||
input: String, | ||
#[label("{message}")] | ||
location: SourceOffset, | ||
} | ||
|
||
impl DecodeError { | ||
pub fn from_serde(input: impl Into<String>, cause: serde_json::Error) -> Self { | ||
let input = input.into(); | ||
let location = SourceOffset::from_location(&input, cause.line(), cause.column()); | ||
DecodeError { | ||
cause, | ||
input, | ||
location, | ||
} | ||
} | ||
|
||
pub fn report(&self) -> String { | ||
use colored::Colorize; | ||
let report = miette::Report::from(ClonedError { | ||
message: self.cause.to_string().italic().to_string(), | ||
input: self.input.clone(), | ||
location: self.location, | ||
}); | ||
|
||
let mut output = String::new(); | ||
|
||
for line in format!("{report:?}").lines().skip(1) { | ||
use std::fmt::Write; | ||
writeln!(&mut output, "{line}").unwrap(); | ||
} | ||
|
||
output.white().to_string() | ||
} | ||
} |
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