Errors

9

אחוז תורגם

בפרק זה:

  • Create a better mechanism for displaying errors and messages.
  • Learn how to use `Template.rendered` to know when a user has seen an error.
  • Use a router filter to make sure errors are only seen once.
  • ////

    ////

    Introducing Local Collections

    ////

    ////

    ////

    ////

    // Local (client-only) collection
    Errors = new Meteor.Collection(null);
    
    client/helpers/errors.js

    ////

    throwError = function(message) {
      Errors.insert({message: message})
    }
    
    client/helpers/errors.js

    ////

    Displaying errors

    ////

    <template name="layout">
      <div class="container">
        {{> header}}
        {{> errors}}
        <div id="main" class="row-fluid">
          {{yield}}
        </div>
      </div>
    </template>
    
    client/views/application/layout.html

    ////

    <template name="errors">
      <div class="errors row-fluid">
        {{#each errors}}
          {{> error}}
        {{/each}}
      </div>
    </template>
    
    <template name="error">
      <div class="alert alert-error">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        {{message}}
      </div>
    </template>
    
    client/views/includes/errors.html

    Twin Templates

    ////

    ////

    ////

    Template.errors.helpers({
      errors: function() {
        return Errors.find();
      }
    });
    
    client/views/includes/errors.js

    Commit 9-1

    Basic error reporting.

    Creating errors

    ////

    ////

    Template.postSubmit.events({
      'submit form': function(e) {
        e.preventDefault();
    
        var post = {
          url: $(e.target).find('[name=url]').val(),
          title: $(e.target).find('[name=title]').val(),
          message: $(e.target).find('[name=message]').val()
        }
    
        Meteor.call('post', post, function(error, id) {
          if (error) {
            // display the error to the user
            throwError(error.reason);
    
            if (error.error === 302)
              Router.go('postPage', {_id: error.details})
          } else {
            Router.go('postPage', {_id: id});
          }
        });
      }
    });
    
    client/views/posts/post_submit.js

    Commit 9-2

    Actually use the error reporting.

    ////

    Triggering an error
    Triggering an error

    Clearing Errors

    ////

    ////

    ////

    ////

    ////

    // Local (client-only) collection
    Errors = new Meteor.Collection(null);
    
    throwError = function(message) {
      Errors.insert({message: message, seen: false})
    }
    
    clearErrors = function() {
      Errors.remove({seen: true});
    }
    
    client/helpers/errors.js

    ////

    // ...
    
    Router.before(requireLogin, {only: 'postSubmit'})
    Router.before(function() { clearErrors() });
    
    lib/router.js

    ////

    ////

    ////

    ////

    ////

    Template.errors.helpers({
      errors: function() {
        return Errors.find();
      }
    });
    
    Template.error.rendered = function() {
      var error = this.data;
      Meteor.defer(function() {
        Errors.update(error._id, {$set: {seen: true}});
      });
    };
    
    client/views/includes/errors.js

    Commit 9-3

    Monitor which errors have been seen, and clear on routing.

    ////

    ////

    The rendered callback

    ////

    ////