Handling Errors
See original GitHub issueProposal 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:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i think we should throw the errors rather than returning it so that it is
switch
statement in thecatch
block on errors, or it can continue to function like it currently doesI 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