From 04d0684d2da625037e6472639d1884fb9a55ac83 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 12 Aug 2012 10:08:43 -0400 Subject: Improve error handling. --- diff --git a/scripts/MarkdownBook/Book.pm b/scripts/MarkdownBook/Book.pm index a3d9eeb..f942803 100644 --- a/scripts/MarkdownBook/Book.pm +++ b/scripts/MarkdownBook/Book.pm @@ -16,6 +16,8 @@ use strict; use warnings; +use Carp; + package MarkdownBook::Book; sub new @@ -36,6 +38,8 @@ sub new use MarkdownBook::Document::Txt; $self->{'format'} = 'txt'; $self->{'format_mod'} = 'Txt'; + } else { + croak('Unsupported format "' . $format . '"'); } $self->{'dir'} = $dir; @@ -114,7 +118,8 @@ sub create_documents # Create chapter documents. $i = 0; - open($series_fh, '<', $self->{'dir'} . '/chapters'); + open($series_fh, '<', $self->{'dir'} . '/chapters') + or croak('Cannot open chapters file'); while (<$series_fh>) { chomp($_); ($file, $title) = split(/[ \t]+/, $_, 2); @@ -131,7 +136,8 @@ sub create_documents $i = -1; @letters = ('A' .. 'Z'); if (-e $self->{'dir'} . '/appendices') { - open($series_fh, '<', $self->{'dir'} . '/apendices'); + open($series_fh, '<', $self->{'dir'} . '/apendices') + or croak('Cannot open appendices file'); while (<$series_fh>) { chomp($_); ($file, $title) = split(/[ \t]+/, $_, 2); @@ -194,11 +200,14 @@ sub _do_subst_macro if ($macro eq 'toc') { return $self->_do_gen_toc(); } elsif ($macro eq 'sectlink') { - die('Invalid arguments to "sectlink" macro') if @args != 1; - $sec = $self->{'sections_by_id'}->{$args[0]}; - return '[§ ' . $sec->number() . '][' . $sec->id() . ']'; + if (@args != 1) { + carp('Invalid arguments to "sectlink" macro'); + } else { + $sec = $self->{'sections_by_id'}->{$args[0]}; + return '[§ ' . $sec->number() . '][' . $sec->id() . ']'; + } } else { - die("Unrecognized macro \"$macro\""); + carp("Unrecognized macro \"$macro\""); } } diff --git a/scripts/MarkdownBook/Document.pm b/scripts/MarkdownBook/Document.pm index b84503d..fa47cdb 100644 --- a/scripts/MarkdownBook/Document.pm +++ b/scripts/MarkdownBook/Document.pm @@ -17,6 +17,7 @@ use strict; use warnings; use MarkdownBook::Section; +use Carp; package MarkdownBook::Document; @@ -143,7 +144,8 @@ sub parse my $source_text; open($source_fh, '<', - $self->{'book'}->dir() . '/' . $self->{'file'} . '.mdwn'); + $self->{'book'}->dir() . '/' . $self->{'file'} . '.mdwn') + or croak('Cannot open "' . $self->{'file'} . '" source document'); $source_text = join('', <$source_fh>); close($source_fh); diff --git a/scripts/MarkdownBook/Document/HTML.pm b/scripts/MarkdownBook/Document/HTML.pm index 9d61760..2852e8e 100644 --- a/scripts/MarkdownBook/Document/HTML.pm +++ b/scripts/MarkdownBook/Document/HTML.pm @@ -18,6 +18,7 @@ use warnings; use MarkdownBook::Document; use MarkdownBook::HTMLTree; +use Carp; use Text::Markdown; use HTML::TreeBuilder; use HTML::Template; @@ -143,7 +144,8 @@ sub _do_output_template $doc_tmpl->param(BODY => $body); - open($doc_fh, '>', $self->file_path() . '.html'); + open($doc_fh, '>', $self->file_path() . '.html') + or croak('Cannot open "' . $self->{'file'} . '" destination document'); $doc_tmpl->output(print_to => $doc_fh); close($doc_fh); } diff --git a/scripts/MarkdownBook/Document/Txt.pm b/scripts/MarkdownBook/Document/Txt.pm index 6c7f038..cf65d92 100644 --- a/scripts/MarkdownBook/Document/Txt.pm +++ b/scripts/MarkdownBook/Document/Txt.pm @@ -17,6 +17,7 @@ use strict; use warnings; use MarkdownBook::Document; +use Carp; package MarkdownBook::Document::Txt; @@ -28,7 +29,8 @@ sub output my $out_fh; open($out_fh, '>', - $self->{'book'}->dir() . '/' . $self->{'file'} . '.txt'); + $self->{'book'}->dir() . '/' . $self->{'file'} . '.txt') + or croak('Cannot open "' . $self->{'file'} . '" destination document'); # Print document title. print($out_fh $self->{'title'} . "\n" . -- cgit v0.9.1