storage: show ObjectHandle.If failed precondition error handling
Documents how one can deal with errors result from
failed preconditions when using ObjectHandle.If.
Fixes
@@ -21,10 +21,12 @@ import (
"io"
"io/ioutil"
"log"
+ "net/http"
"os"
"time"
"cloud.google.com/go/storage"
+ "google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
@@ -680,10 +682,24 @@ func ExampleObjectHandle_If() {
if err != nil {
// TODO: handle error.
}
- defer rc.Close()
+
if _, err := io.Copy(os.Stdout, rc); err != nil {
// TODO: handle error.
}
+ if err := rc.Close(); err != nil {
+ switch ee := err.(type) {
+ case *googleapi.Error:
+ if ee.Code == http.StatusPreconditionFailed {
+ // The condition presented in the If failed.
+ // TODO: handle error.
+ }
+
+ // TODO: handle other status codes here.
+
+ default:
+ // TODO: handle error.
+ }
+ }
}
var secretKey []byte
