Skip to content

Commit

Permalink
Throw error objects instead of strings
Browse files Browse the repository at this point in the history
While JS *technically* allows code to throw anything (including
undefined/null/NaN), it's often considered bad practice.
  • Loading branch information
Gekkio committed Jan 15, 2020
1 parent c82149d commit 930c666
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions lib/passport-saml/saml.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ SAML.prototype.hasValidSignatureForRedirect = function (container, originalQuery
});

if (!hasValidQuerySignature) {
throw 'Invalid signature';
throw new Error('Invalid signature');
}
});
} else {
Expand All @@ -830,7 +830,7 @@ SAML.prototype.validateSignatureForRedirect = function (urlString, signature, al
matchingAlgo = crypto.getHashes()[i];
}
else {
throw alg + ' is not supported';
throw new Error(alg + ' is not supported');
}

var verifier = crypto.createVerify(matchingAlgo);
Expand All @@ -855,7 +855,7 @@ SAML.prototype.verifyLogoutResponse = function (doc) {
return Q.fcall(() => {
var statusCode = doc.LogoutResponse.Status[0].StatusCode[0].$.Value;
if (statusCode !== "urn:oasis:names:tc:SAML:2.0:status:Success")
throw 'Bad status code: ' + statusCode;
throw new Error('Bad status code: ' + statusCode);

this.verifyIssuer(doc.LogoutResponse);
var inResponseTo = doc.LogoutResponse.$.InResponseTo;
Expand All @@ -872,9 +872,9 @@ SAML.prototype.verifyIssuer = function (samlMessage) {
var issuer = samlMessage.Issuer;
if (issuer) {
if (issuer[0]._ !== this.options.idpIssuer)
throw 'Unknown SAML issuer. Expected: ' + this.options.idpIssuer + ' Received: ' + issuer[0]._;
throw new Error('Unknown SAML issuer. Expected: ' + this.options.idpIssuer + ' Received: ' + issuer[0]._);
} else {
throw 'Missing SAML issuer';
throw new Error('Missing SAML issuer');
}
}
};
Expand Down
12 changes: 6 additions & 6 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ describe( 'passport-saml /', function() {
it( 'should throw an error if cert property is provided to saml constructor but is empty', function() {
should(function() {
new SAML( { cert: null } );
}).throw('Invalid property: cert must not be empty');
}).throw({ message: 'Invalid property: cert must not be empty'});
});

it( 'generateUniqueID should generate 20 char IDs', function() {
Expand Down Expand Up @@ -2188,7 +2188,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Unknown SAML issuer. Expected: foo Received: http://localhost:20000/saml2/idp/metadata.php'
);
done();
Expand All @@ -2214,7 +2214,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql('Invalid signature');
err.message.should.eql('Invalid signature');
done();
} catch (err2) {
done(err2);
Expand Down Expand Up @@ -2272,7 +2272,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Unknown SAML issuer. Expected: foo Received: http://localhost:20000/saml2/idp/metadata.php'
);
done();
Expand All @@ -2286,7 +2286,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql(
err.message.should.eql(
'Bad status code: urn:oasis:names:tc:SAML:2.0:status:Requester'
);
done();
Expand All @@ -2312,7 +2312,7 @@ describe( 'passport-saml /', function() {
samlObj.validateRedirect(this.request, this.request.originalQuery, function(err) {
try {
should.exist(err);
err.should.eql('Invalid signature');
err.message.should.eql('Invalid signature');
done();
} catch (err2) {
done(err2);
Expand Down

0 comments on commit 930c666

Please sign in to comment.