1
1
use fastly:: KVStore ;
2
+ use fastly:: kv_store:: KVStoreError ;
2
3
use fastly:: { Error , Request , Response } ;
3
4
4
5
#[ fastly:: main]
@@ -10,21 +11,21 @@ fn main(req: Request) -> Result<Response, Error> {
10
11
}
11
12
12
13
/*
13
- Construct an KVStore instance which is connected to the KV Store named `my-store`
14
+ Construct a KVStore instance which is connected to the KV Store named `my-store`
14
15
15
16
[Documentation for the KVStore open method can be found here](https://docs.rs/fastly/latest/fastly/struct.KVStore.html#method.open)
16
17
*/
17
- let mut store = KVStore :: open ( "my-store" ) . map ( |store| store. expect ( "KVStore exists" ) ) ?;
18
+ let store = KVStore :: open ( "my-store" ) . map ( |store| store. expect ( "KVStore exists" ) ) ?;
18
19
19
20
let path = req. get_path ( ) ;
20
21
if path == "/readme" {
21
- let entry = store. lookup ( "readme" ) ?;
22
+ let mut lookup = match store. lookup ( "readme" ) {
23
+ Ok ( l) => l,
24
+ Err ( KVStoreError :: ItemNotFound ) => return Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) ) ,
25
+ Err ( _e) => return Ok ( Response :: from_body ( "Lookup Error" ) . with_status ( 503 ) ) ,
26
+ } ;
22
27
23
- return match entry {
24
- // Stream the value back to the user-agent.
25
- Some ( entry) => Ok ( Response :: from_body ( entry) ) ,
26
- None => Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) ) ,
27
- } ;
28
+ return Ok ( Response :: from_body ( lookup. take_body ( ) ) ) ;
28
29
} else {
29
30
/*
30
31
Adds or updates the key `hello` in the KV Store with the value `world`.
@@ -44,12 +45,12 @@ fn main(req: Request) -> Result<Response, Error> {
44
45
45
46
[Documentation for the lookup method can be found here](https://docs.rs/fastly/latest/fastly/struct.KVStore.html#method.lookup)
46
47
*/
47
- let entry = store. lookup ( "hello" ) ?;
48
+ let mut lookup = match store. lookup ( "hello" ) {
49
+ Ok ( l) => l,
50
+ Err ( KVStoreError :: ItemNotFound ) => return Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) ) ,
51
+ Err ( _e) => return Ok ( Response :: from_body ( "Lookup Error" ) . with_status ( 503 ) ) ,
52
+ } ;
48
53
49
- return match entry {
50
- // Stream the value back to the user-agent.
51
- Some ( entry) => Ok ( Response :: from_body ( entry) ) ,
52
- None => Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) ) ,
53
- } ;
54
+ return Ok ( Response :: from_body ( lookup. take_body ( ) ) ) ;
54
55
}
55
56
}
0 commit comments