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.

Proposal on how to fix issues like https://github.com/cloudflare/kv-asset-handler/issues/44

Custom Error class

I suggest we create a custom error class that looks something like

export class KVError extends Error {
  constructor(message?: string, code: number = 500) {
    super(message)
    Object.setPrototypeOf(this, new.target.prototype) // restore prototype chain
    this.name = KVError.name // stack traces display correctly now
    this.code = code
  }
  code: number // Appropriate HTTP status code
}

getAssetFromKV throws custom error

getAssetFromKV will now throw custom errors that we can identify as KVError.

Example inside getAssetFromKV

    throw new KVError(`${request.method} is not a valid request method`, 405)
    throw new KVError(`there is no ${ASSET_NAMESPACE} namespace bound to the script`, 500)

Workers sites template

now instead of having crazy try/catch blocks and assuming a 404 error in the sites template, we’d have workers-site-template look like:

try {
  let resp = await getAssetFromKV()
  return new Response(resp.message, { status: resp.code })
} catch (e) {
  if (resp instanceof KVError) {
    switch (e.code) {
      case 404:
        return notFoundResponse
        break
      case 401:
        return methodNotAllowedResponse
        break
      default:
        return new Response(resp.message, { status: resp.code })
    }
  }
}

Alternative option: getAssetFromKV returns a custom error

getAssetFromKV will now not throw generated errors, but instead optionally return KVError.

const getAssetFromKV: (event..) => Promise<KVError | Response>

Examples of returning these errors instead of throwing them:

    return new KVError(`${request.method} is not a valid request method`, 405)
    return new KVError(`there is no ${ASSET_NAMESPACE} namespace bound to the script`, 500)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
EverlastingBugstoppercommented, Nov 25, 2019

i think we should throw the errors rather than returning it so that it is

  1. not a breaking change
  2. you can use a switch statement in the catch block on errors, or it can continue to function like it currently does
0reactions
EverlastingBugstoppercommented, Dec 9, 2019

the question i’ve always had is whether the kv-asset-handler should automatically include the asset found at 404.html (if any) when it throws the NotFound error. developers can use it or not, but that would prevent another round of “was it found?”

I kinda like that - but we should allow the path to be configurable in case people want it to be called something else like notfound.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Exception Handling? - SearchSoftwareQuality
Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events...
Read more >
Handling errors in processes - IBM
Catches specified or all errors · Provides error handling logic for errors raised by activities of the process, subprocess, or event subprocess that...
Read more >
Error Handling — The Swift Programming Language (Swift 5.7)
Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, ...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
Exception handling statements. You can throw exceptions using the throw statement and handle them using the try...catch statements.
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
There are (at least) two distinguishable kinds of errors: syntax error... ... Most exceptions are not handled by programs, however, and result in...
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