-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ux): improve error message for oras manifest push (#1241)
Signed-off-by: Xiaoxuan Wang <[email protected]>
- Loading branch information
1 parent
2ca087a
commit d21c31f
Showing
6 changed files
with
127 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package manifest | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
var ( | ||
// ErrMediaTypeNotFound is returned when the media type is not specified. | ||
ErrMediaTypeNotFound = errors.New(`media type is not specified`) | ||
// ErrInvalidJSON is returned when the json file is malformed. | ||
ErrInvalidJSON = errors.New("not a valid json file") | ||
) | ||
|
||
// ExtractMediaType parses the media type field of bytes content in json format. | ||
func ExtractMediaType(content []byte) (string, error) { | ||
var manifest struct { | ||
MediaType string `json:"mediaType"` | ||
} | ||
if err := json.Unmarshal(content, &manifest); err != nil { | ||
return "", ErrInvalidJSON | ||
} | ||
if manifest.MediaType == "" { | ||
return "", ErrMediaTypeNotFound | ||
} | ||
return manifest.MediaType, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package manifest | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
const ( | ||
manifest = `{"schemaVersion":2,"mediaType":"application/vnd.oci.image.manifest.v1+json","config":{"mediaType":"application/vnd.unknown.config.v1+json","digest":"sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a","size":2},"layers":[{"mediaType":"application/vnd.oci.image.layer.v1.tar","digest":"sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03","size":6,"annotations":{"org.opencontainers.image.title":"hello.txt"}}]}` | ||
manifestMediaType = "application/vnd.oci.image.manifest.v1+json" | ||
) | ||
|
||
func Test_ExtractMediaType(t *testing.T) { | ||
// generate test content | ||
content := []byte(manifest) | ||
|
||
// test ExtractMediaType | ||
want := manifestMediaType | ||
got, err := ExtractMediaType(content) | ||
if err != nil { | ||
t.Fatal("ExtractMediaType() error=", err) | ||
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("ExtractMediaType() = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func Test_ExtractMediaType_invalidContent_notAJson(t *testing.T) { | ||
// generate test content | ||
content := []byte("manifest") | ||
|
||
// test ExtractMediaType | ||
_, err := ExtractMediaType(content) | ||
expected := "not a valid json file" | ||
if err.Error() != expected { | ||
t.Fatalf("ExtractMediaType() error = %v, wantErr %v", err, expected) | ||
} | ||
} | ||
|
||
func Test_ExtractMediaType_invalidContent_missingMediaType(t *testing.T) { | ||
// generate test content | ||
content := []byte(`{"schemaVersion":2}`) | ||
|
||
// test ExtractMediaType | ||
_, err := ExtractMediaType(content) | ||
if !errors.Is(err, ErrMediaTypeNotFound) { | ||
t.Fatalf("ExtractMediaType() error = %v, wantErr %v", err, ErrMediaTypeNotFound) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters