Permalink
Browse files

Adding environment variable support, docs, and tests

  • Loading branch information...
1 parent a18fe17 commit dca508c89669815d34626ab5bdf3153a8460e657 @mattrobenolt mattrobenolt committed Mar 18, 2012
Showing with 92 additions and 4 deletions.
  1. +2 −1 Makefile
  2. +18 −1 README.md
  3. +12 −2 lib/client.js
  4. +60 −0 test/raven.client.js
View
@@ -1,4 +1,5 @@
test:
- @NODE_ENV=test ./node_modules/.bin/mocha --reporter dot
+ @NODE_ENV=test \
+ ./node_modules/.bin/mocha --reporter dot
.PHONY: test
View
@@ -16,6 +16,11 @@ var client = new raven.Client('{{ SENTRY_DSN }}');
client.captureMessage('Hello, world!');
```
+Run with:
+```
+$ NODE_ENV=production node script.js
+```
+
## Logging an error
```javascript
client.captureError(new Error('Broke!'));
@@ -41,6 +46,19 @@ client.captureError(new Error('Broke!'), function(result) {
__Note__: `client.captureMessage` will also return the result directly without the need for a callback, such as: `var result = client.captureMessage('Hello, world!');`
+## Environment variables
+### NODE_ENV
+`NODE_ENV` must be set to `production` for Sentry to actually work. Without being in production, a warning is issued and logging disabled.
+
+### SENTRY_DSN
+Optionally declare the DSN to use for the client through the environment. Initializing the client in your app won't require setting the DSN.
+
+### SENTRY_NAME
+Optionally set the name for the client to use. [What is name?](http://raven.readthedocs.org/en/latest/config/index.html#name)
+
+### SENTRY_SITE
+Optionally set the site for the client to use. [What is site?](http://raven.readthedocs.org/en/latest/config/index.html#site)
+
## Catching global errors
For those times when you don't catch all errors in your application. ;)
@@ -97,7 +115,6 @@ app.listen(3000);
```
## Todo
- * Support for process.env.SENTRY_DSN
* More complete test coverage
* More comments in code
* More third party integration
View
@@ -8,10 +8,20 @@ var parsers = require('./parsers'),
module.exports.version = '0.2.0-dev';
var Client = function Client(dsn, options) {
+ if(arguments.length === 0) {
+ // no arguments, use default from environment
+ dsn = process.env.SENTRY_DSN;
+ options = {};
+ }
+ if(typeof dsn === 'object') {
+ // They must only be passing through options
+ options = dsn;
+ dsn = process.env.SENTRY_DSN;
+ }
options = options || {};
this.dsn = utils.parseDSN(dsn);
- this.name = options.name || require('os').hostname();
- this.site = options.site;
+ this.name = options.name || process.env.SENTRY_NAME || require('os').hostname();
+ this.site = options.site || process.env.SENTRY_SITE;
this.root = options.root || process.cwd();
if(!process.env.NODE_ENV || !(process.env.NODE_ENV == 'production' || process.env.NODE_ENV == 'test')) {
console.log('Warning: Sentry logging is disabled, please set NODE_ENV=production');
View
@@ -22,6 +22,66 @@ describe('raven.Client', function(){
client = new raven.Client(dsn);
});
+ it('should parse the DSN with options', function(){
+ var expected = {
+ protocol: 'https',
+ public_key: 'public',
+ private_key: 'private',
+ host: 'app.getsentry.com',
+ path: '',
+ project_id: 269
+ };
+ var client = new raven.Client(dsn, {name: 'YAY!', site:'Googlez'});
+ client.dsn.should.eql(expected);
+ client.name.should.equal('YAY!');
+ client.site.should.equal('Googlez');
+ });
+
+ it('should pull SENTRY_DSN from environment', function(){
+ var expected = {
+ protocol: 'https',
+ public_key: 'abc',
+ private_key: '123',
+ host: 'app.getsentry.com',
+ path: '',
+ project_id: 1
+ };
+ process.env.SENTRY_DSN='https://abc:123@app.getsentry.com/1';
+ var client = new raven.Client();
+ client.dsn.should.eql(expected);
+ delete process.env.SENTRY_DSN; // gotta clean up so it doesn't leak into other tests
+ });
+
+ it('should pull SENTRY_DSN from environment when passing options', function(){
+ var expected = {
+ protocol: 'https',
+ public_key: 'abc',
+ private_key: '123',
+ host: 'app.getsentry.com',
+ path: '',
+ project_id: 1
+ };
+ process.env.SENTRY_DSN='https://abc:123@app.getsentry.com/1';
+ var client = new raven.Client({name: 'YAY!'});
+ client.dsn.should.eql(expected);
+ client.name.should.equal('YAY!');
+ delete process.env.SENTRY_DSN; // gotta clean up so it doesn't leak into other tests
+ });
+
+ it('should pull SENTRY_NAME from environment', function(){
+ process.env.SENTRY_NAME='new_name';
+ var client = new raven.Client(dsn);
+ client.name.should.eql('new_name');
+ delete process.env.SENTRY_NAME;
+ });
+
+ it('should pull SENTRY_SITE from environment', function(){
+ process.env.SENTRY_SITE='Googlez';
+ var client = new raven.Client(dsn);
+ client.site.should.eql('Googlez');
+ delete process.env.SENTRY_SITE;
+ });
+
describe('#getIdent()', function(){
it('should match', function(){
var result = {

0 comments on commit dca508c

Please sign in to comment.