question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error handling (what's the best way ?)

See original GitHub issue

Hi Jake.

I create my own class called utility where I have functions. Let me clarify two functions of that class.

async writeData(storeName, val, key) {
    const tx = this.db.transaction(storeName, 'readwrite');
    const store = tx.objectStore(storeName);
    store.put(val);
    return await tx.done;
  }


  async readAllData(storeName) {
    const tx = this.db.transaction(storeName, 'readonly');
    const store = tx.objectStore(storeName);
    return await store.getAll();
  }

Now, I use that class in my service worker or in my app. I import this class and use these methods .

Let’s say something like this.

import IndexeddB from './utility.js'

let data = IndexedDB.readAllData('articles');

I am curious about this. If you look at the functions itself, do I have everything set up correctly? I mean I am not using onsuccess and onerror on the request. So return await tx.done (is it fair to say that this will automatically throw if there’s an error and it won’t throw an error if there’s not one, to put this in a better way, where I use those functions, if I put them in try/catch block, it’s all good and I am doing it right?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jakearchibaldcommented, Mar 19, 2020
async writeData(storeName, val, key) {
    const tx = this.db.transaction(storeName, 'readwrite');
    const store = tx.objectStore(storeName);
    store.put(val);
    return await tx.done;
  }

Fwiw, you can just do:

await db.put(storeName, val, key);

And:

const data = await db.getAll(storeName);

See https://github.com/jakearchibald/idb#shortcuts-to-getset-from-an-object-store.

I am curious about this. If you look at the functions itself, do I have everything set up correctly?

Yep, it’s all correct, it’s just avoiding some of the shortcuts.

I mean I am not using onsuccess and onerror on the request.

You don’t need to 😄

So return await tx.done (is it fair to say that this will automatically throw if there’s an error and it won’t throw an error if there’s not one, to put this in a better way, where I use those functions, if I put them in try/catch block, it’s all good and I am doing it right?

Yep, although you don’t really need the await there. See this for details https://jakearchibald.com/2017/await-vs-return-vs-return-await/.

Using the shortcut above:

try {
  const data = await db.getAll(storeName);
  console.log(data);
} catch (err) {
  console.error(err);
}

I’m closing this issue, but feel free to ask follow-up questions.

0reactions
jakearchibaldcommented, May 5, 2020

I’m not really familiar enough with the push API, sorry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Error Handling Done Right - Medium
The strategies to handle error ... Whenever we define an error, we should think through whether the caller has any way to handle...
Read more >
Best practices for error catching and handling
More videos on YouTube · Be very thorough with your error checking · Check for errors first · Handle errors at the first...
Read more >
Clean Code and the Art of Exception Handling - Toptal
Exception Handling : It's a Good Thing · Always create your own ApplicationError hierarchy · Never rescue Exception · Never rescue more exceptions...
Read more >
10 Error Handling Best Practices - CLIMB
1. Don't hide errors from the user · 2. Always log errors · 3. Use exceptions for exceptional cases only · 4. Avoid...
Read more >
Error monitoring and exception handling in large-scale ...
A solution to handle unhandled exceptions with error monitoring · : Prioritize errors/exceptions found on production servers over those found on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found