This repository was archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (35 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
import { response } from 'cfw-easy-utils'
addEventListener('fetch', event => {
event.respondWith(handleEvent(event))
})
async function handleEvent(event) {
const request = event.request
if (request.method == 'GET') {
try {
return await getAssetFromKV(event, { ASSET_NAMESPACE: kv })
} catch (e) {
if (e instanceof NotFoundError) {
// this is what should be happening
var respOut = response.json({"code": 404, "message": "Key does not exist."})
respOut.code = 404
return respOut
} else if (e instanceof MethodNotAllowedError) {
// should never happen
return new Response("this wont ever happen because method was already checked", { status: 500})
} else {
// make sure this exists
console.log(kv)
// i want to know what the error is
return new Response(e.stack, { status: 500})
// if the worker actually worked we would return this instead, or just throw the error
//return new Response('An unexpected error occurred', { status: 500 })
}
}
} else {
// in the complete Worker o would do other operations for different methods
var respOut = response.json({"code": 405, "message": "Method not allowed."})
respOut.code = 405
return respOut
}
}