mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-26 22:23:05 -04:00
As discussed here: https://codeberg.org/forgejo/discussions/issues/444 the container v2 API logic does need some refactoring for better maintainability. This is a proposition on how to achieve that. My goal was to be able to write unit tests for functions like processImageManifest() which are currently only tested indirectly by TestPackageContainer() in tests/integration/api_packages_container_test.go. A first unit test was implemented that targets ProcessManifest(). I think that test also shows what steps are needed to successfully execute the ProcessManifest() function and hopefully helps understanding that code better. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests for Go changes (can be removed for JavaScript changes) - I added test coverage for Go changes... - [x ] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I ran... - [x] `make pr-go` before pushing ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [ x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11432 Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org> Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: patdyn <patdyn@noreply.codeberg.org> Co-committed-by: patdyn <patdyn@noreply.codeberg.org>
52 lines
2 KiB
Go
52 lines
2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package container
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#error-codes
|
|
var (
|
|
ErrBlobUnknown = &NamedError{Code: "BLOB_UNKNOWN", StatusCode: http.StatusNotFound}
|
|
ErrBlobUploadInvalid = &NamedError{Code: "BLOB_UPLOAD_INVALID", StatusCode: http.StatusBadRequest}
|
|
ErrBlobUploadUnknown = &NamedError{Code: "BLOB_UPLOAD_UNKNOWN", StatusCode: http.StatusNotFound}
|
|
ErrDigestInvalid = &NamedError{Code: "DIGEST_INVALID", StatusCode: http.StatusBadRequest}
|
|
ErrManifestBlobUnknown = &NamedError{Code: "MANIFEST_BLOB_UNKNOWN", StatusCode: http.StatusNotFound}
|
|
ErrManifestInvalid = &NamedError{Code: "MANIFEST_INVALID", StatusCode: http.StatusBadRequest}
|
|
ErrManifestUnknown = &NamedError{Code: "MANIFEST_UNKNOWN", StatusCode: http.StatusNotFound}
|
|
ErrNameInvalid = &NamedError{Code: "NAME_INVALID", StatusCode: http.StatusBadRequest}
|
|
ErrNameUnknown = &NamedError{Code: "NAME_UNKNOWN", StatusCode: http.StatusNotFound}
|
|
ErrSizeInvalid = &NamedError{Code: "SIZE_INVALID", StatusCode: http.StatusBadRequest}
|
|
ErrUnauthorized = &NamedError{Code: "UNAUTHORIZED", StatusCode: http.StatusUnauthorized}
|
|
ErrUnsupported = &NamedError{Code: "UNSUPPORTED", StatusCode: http.StatusNotImplemented}
|
|
)
|
|
|
|
type NamedError struct {
|
|
Code string
|
|
StatusCode int
|
|
Message string
|
|
}
|
|
|
|
func (e *NamedError) Error() string {
|
|
return e.Message
|
|
}
|
|
|
|
// WithMessage creates a new instance of the error with a different message
|
|
func (e *NamedError) WithMessage(message string) *NamedError {
|
|
return &NamedError{
|
|
Code: e.Code,
|
|
StatusCode: e.StatusCode,
|
|
Message: message,
|
|
}
|
|
}
|
|
|
|
// WithStatusCode creates a new instance of the error with a different status code
|
|
func (e *NamedError) WithStatusCode(statusCode int) *NamedError {
|
|
return &NamedError{
|
|
Code: e.Code,
|
|
StatusCode: statusCode,
|
|
Message: e.Message,
|
|
}
|
|
}
|