Skip to content

Commit

Permalink
tweak err messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen3H committed Dec 18, 2024
1 parent 74b11bb commit 8046f0c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 40 deletions.
2 changes: 0 additions & 2 deletions experimental/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (b Base64String) String() string {

func GetCommunities() (CommunityList, error) {
res, err := util.JsonGetRequest[CommunitiesResponse]("api/experimental/community")

if err != nil {
return CommunityList{}, err
}
Expand All @@ -32,7 +31,6 @@ func GetCommunities() (CommunityList, error) {
// If the name/id does not match any existing community, the result will be nil.
func GetCommunity(nameOrId string) (*Community, bool, error) {
communities, err := GetCommunities()

if err != nil {
return nil, false, err
}
Expand Down
8 changes: 4 additions & 4 deletions experimental/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AuthResponse struct {
SessionID string `json:"session_id"`
}

// TODO: Implement this.
func LoginWithGithub(auth AuthOptions) (AuthResponse, error) {
return AuthResponse{}, nil
}
// TODO: Implement this. Edit: forgor why I needed this
// func LoginWithGithub(auth AuthOptions) (AuthResponse, error) {
// return AuthResponse{}, nil
// }
6 changes: 0 additions & 6 deletions experimental/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Package struct {
Latest PackageVersion `json:"latest"`
}

// region PackageVersion Struct
type PackageVersion struct {
Namespace string `json:"namespace"`
Name string `json:"name"`
Expand Down Expand Up @@ -53,9 +52,6 @@ func (pkg PackageVersion) getMarkdown(file string) (common.MarkdownResponse, err
return util.JsonGetRequest[common.MarkdownResponse](endpoint)
}

//endregion

// region ReviewStatus Enum
type ReviewStatus string

const (
Expand All @@ -68,8 +64,6 @@ func (rs ReviewStatus) Unreviewed() bool { return rs == UNREVIEWED }
func (rs ReviewStatus) Approved() bool { return rs == APPROVED }
func (rs ReviewStatus) Rejected() bool { return rs == REJECTED }

//endregion

type PackageListing struct {
HasNsfwContent bool `json:"has_nsfw_content"`
Categories string `json:"categories"`
Expand Down
49 changes: 22 additions & 27 deletions experimental/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,74 +35,69 @@ type IconValidatorParams struct {
ImageData []byte
}

func NewErr(msg string) error {
return errors.New(msg)
}

// TODO: Implement this
func SubmitPackage(data []byte) (bool, error) {
return false, nil
}
// func SubmitPackage(data []byte) (bool, error) {
// return false, nil
// }

// TODO: Implement this
func ValidateReadme(data []byte) (bool, error) {
return false, nil
}
// func ValidateReadme(data []byte) (bool, error) {
// return false, nil
// }

func ValidateManifest(author string, data []byte) (bool, []string, error) {
func ValidateManifest(author string, data []byte) (valid bool, errs []string, err error) {
var manifest ManifestMetadata
var errors []string

err := json.Unmarshal(data, &manifest)
err = json.Unmarshal(data, &manifest)
if err != nil {
return false, nil, NewErr("error deserializing manifest: \n" + err.Error())
return false, nil, errors.New("error deserializing manifest: \n" + err.Error())
}

AddIfEmpty(&errors, &manifest.Name, "required property 'name' is empty or unspecified")
AddIfInvalid(&errors, &manifest.Name, "property 'name' must contain only valid characters (a-z A-Z 0-9 _)")
AddIfEmpty(&errors, &manifest.Description, "required property 'description' is empty or unspecified")
AddIfEmpty(&errs, &manifest.Name, "required property 'name' is empty or unspecified")
AddIfInvalid(&errs, &manifest.Name, "property 'name' must contain only valid characters (a-z A-Z 0-9 _)")
AddIfEmpty(&errs, &manifest.Description, "required property 'description' is empty or unspecified")

verEmpty := AddIfEmpty(&errors, &manifest.VersionNumber, "required property 'version_number' is empty or unspecified")
verEmpty := AddIfEmpty(&errs, &manifest.VersionNumber, "required property 'version_number' is empty or unspecified")
if !verEmpty {
valid, _ := util.CheckSemVer(manifest.VersionNumber)
if valid {
matched, _ := util.CheckSemVer(manifest.VersionNumber)
if matched {
pkg, _ := GetPackage(author, manifest.Name)
if pkg != nil {
verA, _ := version.NewSemver(manifest.VersionNumber)
verB, _ := version.NewSemver(pkg.Latest.VersionNumber)

if verA.LessThanOrEqual(verB) {
Add(&errors, "property 'version_number' must be higher than the latest")
Add(&errs, "property 'version_number' must be higher than the latest")
}
}
} else {
Add(&errors, "property 'version_number' does not follow semantic versioning (major.minor.patch)")
Add(&errs, "property 'version_number' does not follow semantic versioning (major.minor.patch)")
}
}

if manifest.WebsiteURL == nil {
Add(&errors, "required property 'website_url' is unspecified")
Add(&errs, "property 'website_url' is empty or unspecified")
} else {
url := strings.ToLower(*manifest.WebsiteURL)
if !(strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
Add(&errors, "property 'website_url' must be a valid URL")
Add(&errs, "property 'website_url' must be a valid URL")
}
}

if manifest.Dependencies == nil {
Add(&errors, "manifest property 'dependencies' is required")
Add(&errs, "required property 'dependencies' is empty or unspecified")
} else {
for _, dep := range manifest.Dependencies {
fullName := author + "-" + manifest.Name
if strings.Contains(strings.ToLower(dep), strings.ToLower(fullName)) {
Add(&errors, "manifest property 'dependencies' is invalid. cannot depend on self")
Add(&errs, "property 'dependencies' is invalid. cannot depend on self")
}

// TODO: Check multiple versions of same package
}
}

return len(errors) < 1, errors, nil
return len(errs) < 1, errs, nil
}

// Decodes image data and validates that the image is a PNG and the dimensions are 256x256.
Expand Down
2 changes: 1 addition & 1 deletion tests/exp/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestValidateIcon(t *testing.T) {
}

func TestValidateManifest(t *testing.T) {
t.Skip()
//t.Skip()

var errs []string

Expand Down

0 comments on commit 8046f0c

Please sign in to comment.