Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support section #133

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
- master

env:
RUST_BACKTRACE: 1
CARGO_TERM_COLOR: always
CLICOLOR: 1

Expand Down Expand Up @@ -45,9 +44,9 @@ jobs:
- name: Test Debug
run: cargo test
- name: Build Release
run: cargo test --no-run --release
run: cargo test --no-run --release --workspace
- name: Test Release
run: cargo test --release
run: cargo test --release --workspace
msrv:
name: "Check MSRV: 1.72"
runs-on: ubuntu-latest
Expand Down
86 changes: 59 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,53 @@ use report::{Method, Report};

use std::borrow::Cow;
use std::io::Result as IoResult;
use std::marker::PhantomData;
use std::panic::PanicInfo;
use std::path::{Path, PathBuf};

pub type MaybeString = Option<Cow<'static, str>>;

/// A convenient metadata struct that describes a crate
///
/// See [`metadata!`]
pub struct Metadata {
/// The crate version
pub version: Cow<'static, str>,
pub version: MaybeString,
/// The crate name
pub name: Cow<'static, str>,
pub name: MaybeString,
/// The list of authors of the crate
pub authors: Cow<'static, str>,
pub authors: MaybeString,
/// The URL of the crate's website
pub homepage: Cow<'static, str>,
pub homepage: MaybeString,
/// The support information
pub support: MaybeString,

// prevent initialized in literal syntax
epage marked this conversation as resolved.
Show resolved Hide resolved
phantom_data: PhantomData<()>,
}

/// Initialize [`Metadata`]
impl Default for Metadata {
epage marked this conversation as resolved.
Show resolved Hide resolved
fn default() -> Self {
Metadata {
version: None,
name: None,
authors: None,
homepage: None,
support: None,
phantom_data: PhantomData,
}
}
}

/// Initialize [`Metadata`].
#[macro_export]
macro_rules! metadata {
() => {{
$crate::Metadata {
version: env!("CARGO_PKG_VERSION").into(),
name: env!("CARGO_PKG_NAME").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
}
let mut metadata = $crate::Metadata::default();
metadata.version = Some(env!("CARGO_PKG_VERSION").into());
metadata.name = Some(env!("CARGO_PKG_NAME").into());
metadata.authors = Some(env!("CARGO_PKG_AUTHORS").replace(":", ", ").into());
metadata
}};
}

Expand All @@ -92,14 +112,13 @@ macro_rules! metadata {
/// `main()` function of the program.
///
/// ```
/// use human_panic::setup_panic;
/// use human_panic::{metadata, setup_panic};
///
/// setup_panic!(Metadata {
/// name: env!("CARGO_PKG_NAME").into(),
/// version: env!("CARGO_PKG_VERSION").into(),
/// authors: "My Company Support <support@mycompany.com>".into(),
/// homepage: "support.mycompany.com".into(),
/// });
/// let mut metadata = metadata!();
epage marked this conversation as resolved.
Show resolved Hide resolved
/// metadata.authors = Some("My Company Support <support@mycompany.com>".into());
/// metadata.homepage = Some("www.mycompany.com".into());
/// metadata.support = Some("- Open a support request by email to support@mycompany.com".into());
/// setup_panic!(metadata);
/// ```
#[macro_export]
macro_rules! setup_panic {
Expand Down Expand Up @@ -180,8 +199,15 @@ fn write_msg<P: AsRef<Path>>(
file_path: Option<P>,
meta: &Metadata,
) -> IoResult<()> {
let (_version, name, authors, homepage) =
(&meta.version, &meta.name, &meta.authors, &meta.homepage);
let Metadata {
name,
authors,
homepage,
support,
..
} = meta;

let name = name.as_deref().unwrap_or("<anon>");

writeln!(buffer, "Well, this is embarrassing.\n")?;
writeln!(
Expand All @@ -201,12 +227,15 @@ fn write_msg<P: AsRef<Path>>(
name
)?;

if !homepage.is_empty() {
if let Some(homepage) = homepage {
writeln!(buffer, "- Homepage: {homepage}")?;
}
if !authors.is_empty() {
if let Some(authors) = authors {
writeln!(buffer, "- Authors: {authors}")?;
}
if let Some(support) = support {
writeln!(buffer, "\nTo submit the crash report:\n\n{support}")?;
}
writeln!(
buffer,
"\nWe take privacy seriously, and do not perform any \
Expand Down Expand Up @@ -235,10 +264,7 @@ pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
(None, None) => None,
};

let cause = match message {
Some(m) => m,
None => "Unknown".into(),
};
let cause = message.unwrap_or_else(|| "Unknown".into());

match panic_info.location() {
Some(location) => expl.push_str(&format!(
Expand All @@ -249,7 +275,13 @@ pub fn handle_dump(meta: &Metadata, panic_info: &PanicInfo) -> Option<PathBuf> {
None => expl.push_str("Panic location unknown.\n"),
}

let report = Report::new(&meta.name, &meta.version, Method::Panic, expl, cause);
let report = Report::new(
meta.name.as_deref().unwrap_or(""),
meta.version.as_deref().unwrap_or(""),
Method::Panic,
expl,
cause,
);

match report.persist() {
Ok(f) => Some(f),
Expand Down
13 changes: 6 additions & 7 deletions tests/custom-panic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use human_panic::setup_panic;
use human_panic::{metadata, setup_panic};

fn main() {
setup_panic!(Metadata {
name: env!("CARGO_PKG_NAME").into(),
version: env!("CARGO_PKG_VERSION").into(),
authors: "My Company Support <support@mycompany.com".into(),
homepage: "support.mycompany.com".into(),
});
let mut metadata = metadata!();
metadata.authors = Some("My Company Support <support@mycompany.com".into());
metadata.homepage = Some("www.mycompany.com".into());
metadata.support = Some("- Open a support request by email to support@mycompany.com".into());
setup_panic!(metadata);

println!("A normal log message");
panic!("OMG EVERYTHING IS ON FIRE!!!");
Expand Down
9 changes: 7 additions & 2 deletions tests/custom-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ custom-panic-test had a problem and crashed. To help us diagnose the problem you
We have generated a report file at \"[..].toml\". Submit an issue or email with the subject of \"custom-panic-test Crash Report\" and include the report as an attachment.
- Homepage: support.mycompany.com
- Homepage: www.mycompany.com
- Authors: My Company Support <support@mycompany.com
To submit the crash report:
- Open a support request by email to support@mycompany.com
We take privacy seriously, and do not perform any automated error collection. In order to improve the software, we rely on people to submit reports.
Thank you kindly!
Expand All @@ -29,7 +33,8 @@ fn debug() {
.assert()
.stderr_matches(
"\
thread 'main' panicked at 'OMG EVERYTHING IS ON FIRE!!!', tests/custom-panic/src/main.rs:12:3
thread 'main' panicked at tests/custom-panic/src/main.rs:13:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
",
)
Expand Down
3 changes: 2 additions & 1 deletion tests/single-panic/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ fn debug() {
.assert()
.stderr_matches(
"\
thread 'main' panicked at 'OMG EVERYTHING IS ON FIRE!!!', tests/single-panic/src/main.rs:7:3
thread 'main' panicked at tests/single-panic/src/main.rs:7:5:
OMG EVERYTHING IS ON FIRE!!!
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
",
)
Expand Down
Loading