Minor refactoring

This commit is contained in:
Marc Ole Bulling
2025-08-29 13:39:43 +02:00
parent 16e4079ffa
commit ff6cc6895f
3 changed files with 21 additions and 21 deletions

View File

@@ -56,7 +56,7 @@ func processUpload(isArchive bool) {
result, err := cliapi.UploadFile(uploadParam)
if err != nil {
fmt.Println()
if errors.Is(cliapi.EUnauthorised, err) {
if errors.Is(cliapi.ErrUnauthorised, err) {
fmt.Println("ERROR: Unauthorised API key. Please re-run login.")
} else {
fmt.Println("ERROR: Could not upload file")

View File

@@ -33,20 +33,20 @@ type header struct {
Value string
}
// EInvalidRequest is returned when the API returns a 400
var EInvalidRequest = errors.New("400 Bad Request")
// ErrInvalidRequest is returned when the API returns a 400
var ErrInvalidRequest = errors.New("400 Bad Request")
// EUnauthorised is returned when the API returns a 401
var EUnauthorised = errors.New("unauthorised")
// ErrUnauthorised is returned when the API returns a 401
var ErrUnauthorised = errors.New("unauthorised")
// ENotFound is returned when the API returns a 404
var ENotFound = errors.New("404 Not Found")
// ErrNotFound is returned when the API returns a 404
var ErrNotFound = errors.New("404 Not Found")
// EFileTooBig is returned when the file size exceeds the allowed limit
var EFileTooBig = errors.New("file too big")
// ErrFileTooBig is returned when the file size exceeds the allowed limit
var ErrFileTooBig = errors.New("file too big")
// EE2eKeyIncorrect is returned when the e2e key is incorrect
var EE2eKeyIncorrect = errors.New("e2e key incorrect")
// ErrE2eKeyIncorrect is returned when the e2e key is incorrect
var ErrE2eKeyIncorrect = errors.New("e2e key incorrect")
// Init initialises the API client with the given url and key.
// The key is used for authentication.
@@ -123,11 +123,11 @@ func getUrl(url string, headers []header, longTimeout bool) (string, error) {
switch resp.StatusCode {
case 400:
return "", EInvalidRequest
return "", ErrInvalidRequest
case 401:
return "", EUnauthorised
return "", ErrUnauthorised
case 404:
return "", ENotFound
return "", ErrNotFound
}
body, err := io.ReadAll(resp.Body)
@@ -165,7 +165,7 @@ func UploadFile(uploadParams cliflags.UploadConfig) (models.FileApiOutput, error
sizeBytes = encryption.CalculateEncryptedFilesize(sizeBytes)
}
if sizeBytes > int64(maxSize)*megaByte {
return models.FileApiOutput{}, EFileTooBig
return models.FileApiOutput{}, ErrFileTooBig
}
uuid := helper.GenerateRandomString(30)
@@ -345,7 +345,7 @@ func GetE2eInfo() (models.E2EInfoPlainText, error) {
}
fileInfo, err = end2end.DecryptData(result, e2eKey)
if err != nil {
return models.E2EInfoPlainText{}, EE2eKeyIncorrect
return models.E2EInfoPlainText{}, ErrE2eKeyIncorrect
}
return fileInfo, nil
}

View File

@@ -47,16 +47,16 @@ func CreateLogin() {
vstr, vint, err := cliapi.GetVersion()
if err != nil {
fmt.Println()
if errors.Is(cliapi.EUnauthorised, err) {
if errors.Is(cliapi.ErrUnauthorised, err) {
fmt.Println("ERROR: Unauthorised API key")
os.Exit(1)
}
if errors.Is(cliapi.ENotFound, err) {
if errors.Is(cliapi.ErrNotFound, err) {
fmt.Println("ERROR: API not found")
fmt.Println("The provided URL does not respond to API calls as expected. You most likely entered an incorrect URL.")
os.Exit(1)
}
if errors.Is(cliapi.EInvalidRequest, err) {
if errors.Is(cliapi.ErrInvalidRequest, err) {
fmt.Println("ERROR: API does not support Gokapi CLI")
fmt.Println("This is most likely caused by an old Gokapi version. Please make sure that your Gokapi instance is running v2.1.0 or newer.")
os.Exit(1)
@@ -75,7 +75,7 @@ func CreateLogin() {
_, _, isE2E, err := cliapi.GetConfig()
if err != nil {
fmt.Println("FAIL")
if errors.Is(cliapi.EUnauthorised, err) {
if errors.Is(cliapi.ErrUnauthorised, err) {
fmt.Println("ERROR: API key does not have the permission to upload new files.")
} else {
fmt.Println(err)
@@ -95,7 +95,7 @@ func CreateLogin() {
cliapi.Init(url, apikey, e2ekey)
_, err = cliapi.GetE2eInfo()
if err != nil {
if errors.Is(cliapi.EE2eKeyIncorrect, err) {
if errors.Is(cliapi.ErrE2eKeyIncorrect, err) {
fmt.Println("ERROR: Incorrect end-to-end encryption key")
} else {
fmt.Println(err)