helpers

package
v0.5.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2026 License: BSD-2-Clause Imports: 21 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrDocumentIsRevoked is returned when a document is revoked
	ErrDocumentIsRevoked = NewError("DOCUMENT_IS_REVOKED")
	// ErrNoTransactionID is returned when transactionID is not present
	ErrNoTransactionID = NewError("NO_TRANSACTION_ID")

	// ErrNoDocumentFound is returned when no document is found
	ErrNoDocumentFound = NewError("NO_DOCUMENT_FOUND")

	// ErrDocumentAlreadyExists is returned when a document already exists
	ErrDocumentAlreadyExists = NewError("DOCUMENT_ALREADY_EXISTS")

	// ErrNoDocumentData is returned when no document_data is found
	ErrNoDocumentData = NewError("NO_DOCUMENT_DATA")

	// ErrNoIdentityFound is returned when no identity is found
	ErrNoIdentityFound = NewError("NO_IDENTITY_FOUND")

	// ErrDuplicateKey is returned when a duplicate key is found
	ErrDuplicateKey = NewError("DUPLICATE_KEY")

	// ErrNoRevocationID is returned when no revocation_id is found
	ErrNoRevocationID = NewError("NO_REVOCATION_ID")

	// ErrPrivateKeyMissing error for empty private key
	ErrPrivateKeyMissing = NewError("ERR_PRIVATE_KEY_MISSING")

	// ErrNoKnownVCT error for no known vct
	ErrNoKnownVCT = NewError("ERR_NO_KNOWN_VCT")

	// ErrInternalServerError error for internal server error
	ErrInternalServerError = NewError("INTERNAL_SERVER_ERROR")

	// ErrDocumentValidationFailed error for document validation failed
	ErrDocumentValidationFailed = NewError("DOCUMENT_VALIDATION_FAILED")
)

Functions

func Check

func Check(ctx context.Context, cfg *model.Cfg, s any, log *logger.Log) error

Check checks for validation error

func CheckSimple

func CheckSimple(s any) error

CheckSimple checks for validation error with a simpler signature

func HostFromURL

func HostFromURL(rawURL string) (string, error)

HostFromURL extracts the host (including any port) from a URL string. Unlike strings.TrimLeft, this correctly handles all URL schemes and paths.

Example
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	host, err := helpers.HostFromURL("https://example.com:8080/path")
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(host)
}
Output:
example.com:8080
Example (NoHost)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	_, err := helpers.HostFromURL("not-a-url")
	fmt.Println("error:", err)
}
Output:
error: URL "not-a-url" has no host
Example (WithoutPort)
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	host, err := helpers.HostFromURL("https://example.com/path/to/resource")
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(host)
}
Output:
example.com

func NewValidator

func NewValidator() (*validator.Validate, error)

NewValidator creates a new validator

func Problem404

func Problem404() *problems.Problem

func ValidateDocumentData

func ValidateDocumentData(ctx context.Context, completeDocument *model.CompleteDocument, log *logger.Log) error

ValidateDocumentData validates DocumentData against the schemaRef in MetaData.DocumentDataValidationRef

Types

type Error

type Error struct {
	Title      string `json:"title"`
	Err        any    `json:"details"`
	HTTPStatus int    `json:"-"` // HTTP status code to return, 0 means auto-detect
}

Error is a struct that represents an error

func NewError

func NewError(title string) *Error
Example
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	err := helpers.NewError("NOT_FOUND")
	fmt.Println(err)
}
Output:
Error: [NOT_FOUND]

func NewErrorDetails

func NewErrorDetails(title string, err any) *Error
Example
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	err := helpers.NewErrorDetails("VALIDATION_FAILED", "field 'name' is required")
	fmt.Println(err)
}
Output:
Error: [VALIDATION_FAILED] field 'name' is required

func NewErrorDetailsWithStatus

func NewErrorDetailsWithStatus(title string, err any, httpStatus int) *Error

NewErrorDetailsWithStatus creates a new Error with details and an explicit HTTP status code

func NewErrorFromError

func NewErrorFromError(err error) *Error

NewErrorFromError creates a new Error from an error

func NewErrorWithStatus

func NewErrorWithStatus(title string, httpStatus int) *Error

NewErrorWithStatus creates a new Error with an explicit HTTP status code

Example
package main

import (
	"fmt"

	"github.com/SUNET/vc/pkg/helpers"
)

func main() {
	err := helpers.NewErrorWithStatus("UNAUTHORIZED", 401)
	fmt.Println(err)
	fmt.Println("status:", err.HTTPStatus)
}
Output:
Error: [UNAUTHORIZED]
status: 401

func (*Error) Error

func (e *Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Error *Error `json:"error"`
}

ErrorResponse is a struct that represents an error response in JSON from REST API

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL