1 // !code vendor/crayon/lib/template.js
  2 
  3 // The default reasons are from apache http server error docs.
  4 Crayon.Error = {
  5    BAD_REQUEST : {
  6       error: "Bad Request",
  7       code : 400,
  8       reason : "Your browser (or proxy) sent a request that this server could not understand."
  9    },
 10    UNAUTHORIZED: {
 11       error: "Unauthorized",
 12       code: 401,
 13       reason: "In case you are allowed to request the document, please check your user-id and password and try again."
 14    },
 15    FORBIDDEN: {
 16       error: "Forbidden",
 17       code: 403,
 18       reason: "You don't have permission to access the requested object. It is either read-protected or not readable by the server."
 19    },
 20    NOT_FOUND: {
 21       error: "Not Found",
 22       code: 404,
 23       reason: "The requested URL was not found on this server."
 24    },
 25    INTERNAL_SERVER_ERROR : {
 26       error: "Internal Server Error",
 27       code: 500,
 28       reason: "The server encountered an internal error and was unable to complete your request."
 29    },
 30    NOT_IMPLEMENTED: {
 31       error: "Not Implemented",
 32       code: 501,
 33       reason: "The server does not support the action requested by the browser."
 34    },
 35    SERVICE_UNAVAILABLE: {
 36       error: "Service Temporarily Unavailable",
 37       code: 503,
 38       reason : "The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later. "
 39    },
 40 
 41    render_error : function(){
 42       var ret = Crayon.extractOptions.apply(this, arguments);
 43       if( arguments.length == 1 ){
 44          ret.args.push(ret.options);
 45          ret.options = null;
 46       }
 47       var options = ret.options || {};
 48       var error = {
 49          error  : ret.args[0].error,
 50          code   : ret.args[0].code,
 51          reason : options.reason || ret.args[0].reason
 52       };
 53       var format = options.format;
 54       var template = options.template;
 55       var bindings = options.bindings || {};
 56       if( !bindings.error ){
 57          bindings.error = error;
 58       }
 59 
 60       if( template ){
 61          return {
 62             code : error.code,
 63             body : render(template, bindings)
 64          };
 65       }else{
 66          switch(format){
 67          case "html":
 68             return {
 69                code: error.code,
 70                body: render("<html><title><%= error.error %></title><body><p><%= error.reason %><p></body></html>",
 71                             {
 72                                error: error
 73                             })
 74             };
 75          default:
 76             return {
 77                code: error.code,
 78                body: toJSON(error)
 79             };
 80          }
 81       }
 82    }
 83 };
 84 
 85 if( !this.do_not_import_global ){
 86    Crayon.extend(this, Crayon.Error);
 87 }else{
 88    Crayon.extend(Crayon.Error);
 89 }