|
1 | 1 | import { Server } from 'SERVER';
|
2 | 2 | import { manifest, prerendered } from 'MANIFEST';
|
3 |
| -import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'; |
| 3 | +import { |
| 4 | + getAssetFromKV, |
| 5 | + mapRequestToAsset, |
| 6 | + MethodNotAllowedError, |
| 7 | + NotFoundError |
| 8 | +} from '@cloudflare/kv-asset-handler'; |
4 | 9 | import static_asset_manifest_json from '__STATIC_CONTENT_MANIFEST';
|
5 | 10 | const static_asset_manifest = JSON.parse(static_asset_manifest_json);
|
6 | 11 |
|
@@ -86,19 +91,29 @@ export default {
|
86 | 91 | * @param {any} context
|
87 | 92 | */
|
88 | 93 | async function get_asset_from_kv(req, env, context, map = mapRequestToAsset) {
|
89 |
| - return await getAssetFromKV( |
90 |
| - { |
91 |
| - request: req, |
92 |
| - waitUntil(promise) { |
93 |
| - return context.waitUntil(promise); |
| 94 | + try { |
| 95 | + return await getAssetFromKV( |
| 96 | + { |
| 97 | + request: req, |
| 98 | + waitUntil(promise) { |
| 99 | + return context.waitUntil(promise); |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + ASSET_NAMESPACE: env.__STATIC_CONTENT, |
| 104 | + ASSET_MANIFEST: static_asset_manifest, |
| 105 | + mapRequestToAsset: map |
94 | 106 | }
|
95 |
| - }, |
96 |
| - { |
97 |
| - ASSET_NAMESPACE: env.__STATIC_CONTENT, |
98 |
| - ASSET_MANIFEST: static_asset_manifest, |
99 |
| - mapRequestToAsset: map |
| 107 | + ); |
| 108 | + } catch (e) { |
| 109 | + if (e instanceof NotFoundError) { |
| 110 | + return new Response('Not found', { status: 404 }); |
| 111 | + } else if (e instanceof MethodNotAllowedError) { |
| 112 | + return new Response('Method not allowed', { status: 405 }); |
| 113 | + } else { |
| 114 | + return new Response('Internal Error', { status: 500 }); |
100 | 115 | }
|
101 |
| - ); |
| 116 | + } |
102 | 117 | }
|
103 | 118 |
|
104 | 119 | /**
|
|
0 commit comments