if a commodity price cannot be downloaded, report it as an error

This commit is contained in:
John Wiegley 2004-09-25 02:51:55 -04:00
parent fbad042178
commit 3a3227298d
6 changed files with 29 additions and 15 deletions

View file

@ -10,7 +10,7 @@
namespace ledger {
static unsigned long binary_magic_number = 0xFFEED765;
static unsigned long format_version = 0x0002001b;
static unsigned long format_version = 0x00020020;
static account_t ** accounts;
static account_t ** accounts_next;

View file

@ -8,6 +8,7 @@ using namespace ledger;
#include <sstream>
#include <memory>
#include <algorithm>
#include <exception>
#include <iterator>
#include <string>
#include <cstdlib>
@ -349,7 +350,7 @@ int main(int argc, char * argv[], char * envp[])
try {
return parse_and_report(argc, argv, envp);
}
catch (error& err) {
catch (const std::exception& err) {
std::cerr << "Error: " << err.what() << std::endl;
return 1;
}

View file

@ -111,8 +111,7 @@ void process_arguments(std::list<option_t>& options,
goto next;
}
std::cerr << "Error: illegal option " << *i << std::endl;
std::exit(1);
throw option_error(std::string("illegal option ") + *i);
} else {
for (std::list<option_t>::iterator j = options.begin();
j != options.end();
@ -132,8 +131,7 @@ void process_arguments(std::list<option_t>& options,
}
}
std::cerr << "Error: illegal option -- " << (*i)[1] << std::endl;
std::exit(1);
throw option_error(std::string("illegal option -- ") + (*i)[1]);
}
next:

View file

@ -3,6 +3,7 @@
#include <list>
#include <string>
#include <exception>
struct option_handler {
bool handled;
@ -19,6 +20,17 @@ struct option_t {
option_t() : short_opt(0), wants_arg(false), handler(NULL) {}
};
class option_error : public std::exception {
std::string reason;
public:
option_error(const std::string& _reason) throw() : reason(_reason) {}
virtual ~option_error() throw() {}
virtual const char* what() const throw() {
return reason.c_str();
}
};
void add_option_handler(std::list<option_t>& options, const std::string& label,
const std::string& opt_chars, option_handler& option);
bool process_option(std::list<option_t>& options,

View file

@ -1,5 +1,6 @@
#include "quotes.h"
#include "datetime.h"
#include "error.h"
#include "debug.h"
#include <fstream>
@ -32,10 +33,6 @@ void quotes_by_script::operator()(commodity_t& commodity,
DEBUG_PRINT_("downloading quote for symbol " << commodity.symbol);
// Only consult the Internet once for any commodity
commodity.last_lookup = now;
cache_dirty = true;
char buf[256];
buf[0] = '\0';
@ -45,7 +42,8 @@ void quotes_by_script::operator()(commodity_t& commodity,
commodity.symbol).c_str(), "r")) {
if (feof(fp) || ! fgets(buf, 255, fp))
success = false;
fclose(fp);
if (pclose(fp) != 0)
success = false;
}
if (success && buf[0]) {
@ -57,6 +55,9 @@ void quotes_by_script::operator()(commodity_t& commodity,
price.parse(buf);
commodity.add_price(now, price);
commodity.last_lookup = now;
cache_dirty = true;
if (price && ! price_db.empty()) {
char buf[128];
strftime(buf, 127, "%Y/%m/%d %H:%M:%S", localtime(&now));
@ -64,6 +65,10 @@ void quotes_by_script::operator()(commodity_t& commodity,
database << "P " << buf << " " << commodity.symbol
<< " " << price << endl;
}
} else {
throw error(std::string("Failed to download price for '") +
commodity.symbol + "' (command: \"getquote " +
commodity.symbol + "\")");
}
}

View file

@ -1,16 +1,14 @@
#!/usr/bin/perl
exit 0 if $ARGV[0] eq "\$";
use Finance::Quote;
$q = Finance::Quote->new;
$q->timeout(60);
$q->require_labels(qw/price/);
%quotes = $q->fetch("nasdaq", $ARGV[0]);
if ($quotes{$ARGV[0], "price"}) {
print "\$", $quotes{$ARGV[0], "price"}, "\n";
} else {
exit 1;
}