Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit c4e0737

Browse files
committed
Slightly improve an error message
This reminds me to review the error reporting and make it more consistent. The InnerError should ideally not carry data at all but be concrete error cases we want to set as contexts.
1 parent 633cc6c commit c4e0737

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ impl Display for Error {
2929

3030
#[derive(Fail, Debug)]
3131
enum InnerError {
32+
#[fail(display = "{}", _0)]
33+
Custom(Context<String>),
34+
3235
#[fail(display = "IO error: {}", _0)]
3336
Io(io::Error),
3437

@@ -66,6 +69,22 @@ impl Error {
6669
}
6770
}
6871

72+
impl From<InnerError> for Error {
73+
fn from(kind: InnerError) -> Error {
74+
Error {
75+
inner: Context::new(kind),
76+
}
77+
}
78+
}
79+
80+
impl From<Context<String>> for Error {
81+
fn from(inner: Context<String>) -> Error {
82+
Error {
83+
inner: Context::new(InnerError::Custom(inner)),
84+
}
85+
}
86+
}
87+
6988
impl From<io::Error> for Error {
7089
fn from(x: io::Error) -> Self {
7190
Error {

src/json.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! JSON output
22
33
use crate::{Error, Target};
4+
use failure::ResultExt;
45
use serde::Serialize;
56
use serde_json::to_vec as write_json;
67
use std::io::Write;
@@ -23,7 +24,11 @@ pub fn file<T: AsRef<Path>>(name: T) -> Result<Target, Error> {
2324
use std::io::BufWriter;
2425

2526
let target = if path.exists() {
26-
let mut f = OpenOptions::new().write(true).append(true).open(&path)?;
27+
let mut f = OpenOptions::new()
28+
.write(true)
29+
.append(true)
30+
.open(&path)
31+
.with_context(|_| format!("Can't open file `{}` as JSON target", path.display()))?;
2732
f.write_all(b"\n")?;
2833

2934
f

0 commit comments

Comments
 (0)