Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Throw proper Error objects #4

Merged
merged 1 commit into from

2 participants

Marco Rogers Kieran Huggins
Marco Rogers

... otherwise you don't get a stack trace. This bit me today.

Kieran Huggins kieran merged commit 563fc2a into from
Kieran Huggins
Owner

Wicked, thanks :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 16, 2012
  1. Marco Rogers

    throw proper Error objects

    polotek authored
This page is out of date. Refresh to see the latest.
Showing with 9 additions and 9 deletions.
  1. +5 −5 lib/route.js
  2. +4 −4 lib/router.js
10 lib/route.js
View
@@ -40,7 +40,7 @@ Route.prototype.PARTS = /\([^)]+\)|:[a-zA-Z_][\w\-]*|\*[a-zA-Z_][\w\-]*|[\w\-_\\
//
Route.prototype.match = function( router, path, method, optional ){
- if ( typeof path != 'string' ) throw 'path must be a string'
+ if ( typeof path != 'string' ) throw new Error('path must be a string')
// is this a nested path?
if ( this.path != undefined ) {
@@ -216,7 +216,7 @@ Route.prototype.to = function( endpoint, extra_params ) {
if ( endpoint ){
endpoint = endpoint.split('.')
- if( kindof(endpoint) == 'array' && endpoint.length != 2 ) throw 'syntax should be in the form: controller.action'
+ if( kindof(endpoint) == 'array' && endpoint.length != 2 ) throw new Error('syntax should be in the form: controller.action')
this.params.controller = endpoint[0]
this.params.action = endpoint[1]
}
@@ -253,7 +253,7 @@ Route.prototype.name = function( name ) {
//
Route.prototype.where = function( conditions ) {
- if ( kindof(conditions) != 'object' ) throw 'conditions must be an object'
+ if ( kindof(conditions) != 'object' ) throw new Error('conditions must be an object')
for (var i in this.parts) {
if (this.parts[i] instanceof Key || this.parts[i] instanceof Route) {
@@ -439,7 +439,7 @@ Route.prototype.parse = function( urlParam, method ) {
Route.prototype.nest = function(cb){
- if ( typeof cb != 'function' ) throw 'route.nest() requires a callback function'
+ if ( typeof cb != 'function' ) throw new Error('route.nest() requires a callback function')
cb.call( this )
return this // for chaining
-}
+}
8 lib/router.js
View
@@ -42,7 +42,7 @@ Router.prototype.match = function( path, method ) {
if ( typeof method == 'string' ) method = method.toUpperCase()
- if ( typeof method != 'undefined' && !this.methods_regexp().test(method)) throw 'method must be one of: ' + this.METHODS.join(', ')
+ if ( typeof method != 'undefined' && !this.methods_regexp().test(method)) throw new Error('method must be one of: ' + this.METHODS.join(', '))
var route = new Route( this, path, method)
// this.routes.push(route)
@@ -240,12 +240,12 @@ Router.prototype.methods_regexp = function(){
// **THIS IS CURRENTLY COMPLETELY UNTESTED. IT MIGHT NOT EVEN WORK. SERIOUSLY.**
//
Router.prototype.defer = function( fn ) {
- if ( typeof(fn) != 'function' ) throw 'Router.defer requires a function as the only argument'
+ if ( typeof(fn) != 'function' ) throw new Error('Router.defer requires a function as the only argument')
var route = new Route(this, 'deferred')
route.parse = fn // add the custom parser
delete route.test // = function(){return false};
- delete route.stringify //= function(){ throw 'Deferred routes are NOT generatable'};
+ delete route.stringify //= function(){ throw new Error('Deferred routes are NOT generatable')};
this.routes.push(route)
return route
}
@@ -253,4 +253,4 @@ Router.prototype.defer = function( fn ) {
Router.prototype.list = function(){
console.log(this.routes.map(function(rt){return rt.path}))
-}
+}
Something went wrong with that request. Please try again.