Skip to content

Optimistic Error Handling Got TypeError Not NetworkError #989

@hendra

Description

@hendra

When implement the optimistic strategies for OrbitJS offline I always got error "TypeError" instead of "NetworkError" inside the RequestStrategy updateFail action block.

image

Here is my coordinator code (see my comment inside the RequestStrategy updateFail block) :

// Back up data to IndexedDB
coordinator.addStrategy(
  new SyncStrategy({
    source: "memory",
    target: "backup",
    blocking: true,
  })
);

// Sync all changes received from the remote server to the memory source
coordinator.addStrategy(
  new SyncStrategy({
    source: "remote",
    target: "memory",
    blocking: true,
  })
);

// Query the remote server whenever the memory source is queried
coordinator.addStrategy(
  new RequestStrategy({
    source: "memory",
    target: "remote",
    on: "beforeQuery",
    action: "query",
    passHints: false,
    blocking: false,
    catch() {
      this.target.requestQueue.skip();
    },
  })
);

// Update the remote server whenever the memory source is updated
coordinator.addStrategy(
  new RequestStrategy({
    source: "memory",
    on: "beforeUpdate",
    target: "remote",
    action: "update",
    passHints: false,
    blocking: false
  })
);

coordinator.addStrategy(
  new RequestStrategy({
    source: "remote",
    on: "updateFail",
    action(transform, e) {
      const source = this.source;
      const store = this.coordinator.getSource("memory");

      // The problem is here, 
      // this if block never gets call when there is network connection problem
      // because the e object is instanceof TypeError and not NetworkError
      if (e instanceof NetworkError) {
        // When network errors are encountered, try again in 3s
        console.log("NetworkError - will try again soon");
        setTimeout(() => {
          source.requestQueue.retry();
        }, 3000);
      } else {
        // When non-network errors occur, notify the user and
        // reset state.
        let label = transform.options && transform.options.label;
        if (label) {
          console.log(`Unable to complete "${label}"`);
        } else {
          console.log(`Unable to complete operation`);
        }

        // Roll back store to position before transform
        if (store.transformLog.contains(transform.id)) {
          console.log("Rolling back - transform:", transform.id);
          store.rollback(transform.id, -1);
        }

        return source.requestQueue.skip();
      }
    }
  })
);

coordinator.addStrategy(
  new RequestStrategy({
    source: "remote",
    on: "queryFail",
    action() {
      this.source.requestQueue.skip();
    },
  })
);

I am using OrbitJS V0.17 and I am following the sample implementation from here https://github.com/orbitjs/todomvc-ember-orbit#scenario-4-memory--backup--remote. Anyone knows why this happened ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions