Commit 007857af56030fbe4c85f43162679572662a396d
BCrypt::Errors::* descend from new BCrypt::Error.
This makes error handling far easier, and does not impact backwards compatibility. This is now possible: begin # some BCrypt call rescue BCrypt::Error # handle error end Instead of: begin # some BCrypt call rescue BCrypt::Errors::InvalidSalt, BCrypt::Errors::InvalidHash, BCrypt::Errors::InvalidCost, BCrypt::Errors::InvalidSecret # handle error end RSpec output: Errors BCrypt::Error can be rescued as a StandardError BCrypt::Errors::InvalidCost can be rescued as a BCrypt::Error BCrypt::Errors::InvalidHash can be rescued as a BCrypt::Error BCrypt::Errors::InvalidSalt can be rescued as a BCrypt::Error BCrypt::Errors::InvalidSecret can be rescued as a BCrypt::ErrorPaul Annesley committed on 9/28/2012, 5:07:14 AM
Parent: 7a0667dce1c44b0f4b19a6edaf39f93e54121d5f
Files changed
lib/bcrypt.rb | changed |
spec/bcrypt/error_spec.rb | changed |
lib/bcrypt.rb | ||
---|---|---|
@@ -10,13 +10,15 @@ | ||
10 | 10 | |
11 | 11 | # A Ruby library implementing OpenBSD's bcrypt()/crypt_blowfish algorithm for |
12 | 12 | # hashing passwords. |
13 | 13 | module BCrypt |
14 | + | |
15 | + class Error < StandardError; end | |
14 | 16 | module Errors |
15 | - class InvalidSalt < StandardError; end # The salt parameter provided to bcrypt() is invalid. | |
16 | - class InvalidHash < StandardError; end # The hash parameter provided to bcrypt() is invalid. | |
17 | - class InvalidCost < StandardError; end # The cost parameter provided to bcrypt() is invalid. | |
18 | - class InvalidSecret < StandardError; end # The secret parameter provided to bcrypt() is invalid. | |
17 | + class InvalidSalt < BCrypt::Error; end # The salt parameter provided to bcrypt() is invalid. | |
18 | + class InvalidHash < BCrypt::Error; end # The hash parameter provided to bcrypt() is invalid. | |
19 | + class InvalidCost < BCrypt::Error; end # The cost parameter provided to bcrypt() is invalid. | |
20 | + class InvalidSecret < BCrypt::Error; end # The secret parameter provided to bcrypt() is invalid. | |
19 | 21 | end |
20 | 22 | |
21 | 23 | # A Ruby wrapper for the bcrypt() C extension calls and the Java calls. |
22 | 24 | class Engine |
spec/bcrypt/error_spec.rb | ||
---|---|---|
@@ -1,27 +1,37 @@ | ||
1 | 1 | require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) |
2 | 2 | |
3 | 3 | describe "Errors" do |
4 | 4 | |
5 | - shared_examples "StandardError" do | |
6 | - it "is is rescued as a StandardError" do | |
5 | + shared_examples "descends from StandardError" do | |
6 | + it "can be rescued as a StandardError" do | |
7 | 7 | described_class.should < StandardError |
8 | 8 | end |
9 | 9 | end |
10 | 10 | |
11 | + shared_examples "descends from BCrypt::Error" do | |
12 | + it "can be rescued as a BCrypt::Error" do | |
13 | + described_class.should < BCrypt::Error | |
14 | + end | |
15 | + end | |
16 | + | |
17 | + describe BCrypt::Error do | |
18 | + include_examples "descends from StandardError" | |
19 | + end | |
20 | + | |
11 | 21 | describe BCrypt::Errors::InvalidCost do |
12 | - it_behaves_like "StandardError" | |
22 | + include_examples "descends from BCrypt::Error" | |
13 | 23 | end |
14 | 24 | |
15 | 25 | describe BCrypt::Errors::InvalidHash do |
16 | - it_behaves_like "StandardError" | |
26 | + include_examples "descends from BCrypt::Error" | |
17 | 27 | end |
18 | 28 | |
19 | 29 | describe BCrypt::Errors::InvalidSalt do |
20 | - it_behaves_like "StandardError" | |
30 | + include_examples "descends from BCrypt::Error" | |
21 | 31 | end |
22 | 32 | |
23 | 33 | describe BCrypt::Errors::InvalidSecret do |
24 | - it_behaves_like "StandardError" | |
34 | + include_examples "descends from BCrypt::Error" | |
25 | 35 | end |
26 | 36 | |
27 | 37 | end |
Built with git-ssb-web