1
+ use fastly:: kv_store:: KVStoreError ;
1
2
use fastly:: KVStore ;
2
3
use fastly:: { Error , Request , Response } ;
3
4
@@ -10,20 +11,20 @@ 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
-
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 ) ) ,
22
+ return match store. lookup ( "readme" ) {
23
+ Ok ( mut l ) => Ok ( Response :: from_body ( l . take_body ( ) ) ) ,
24
+ Err ( KVStoreError :: ItemNotFound ) => {
25
+ return Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) )
26
+ }
27
+ Err ( _e ) => return Ok ( Response :: from_body ( "Lookup Error " ) . with_status ( 503 ) ) ,
27
28
} ;
28
29
} else {
29
30
/*
@@ -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
-
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 ) ) ,
48
+ return match store. lookup ( "hello" ) {
49
+ Ok ( mut l ) => Ok ( Response :: from_body ( l . take_body ( ) ) ) ,
50
+ Err ( KVStoreError :: ItemNotFound ) => {
51
+ return Ok ( Response :: from_body ( "Not Found" ) . with_status ( 404 ) )
52
+ }
53
+ Err ( _e ) => return Ok ( Response :: from_body ( "Lookup Error " ) . with_status ( 503 ) ) ,
53
54
} ;
54
55
}
55
56
}
0 commit comments