Skip to Content
go-dicom(3)
Packages

errors

A small family of error types with a code and details, returned by charset and encaps. Most errors you will handle are not these: each package exports its own sentinel errors and types, tested with the standard errors.Is and errors.As.

import "github.com/amrshadid/go-dicom/errors" · pkg.go.dev

When to use it

  • Tell a character set failure from charset (*DicomUnicodeDecodeError) or an encapsulation failure from encaps (*EncapsulationError) apart from other errors, and read its code.

For everything else, test the error the package that returned it documents. The common ones:

PackageErrors
filereaderErrValueTooLong, ErrHeaderTooLarge, ErrTruncated, *ReadError and ErrorKind
datasetErrUndecodedSyntax, ErrNoEncoder, ErrLossyNotAllowed, ErrPixelSizeOverflow, *ICCProfileError
compressErrFrameMismatch, ErrJPEGXLNotDecoded
network*StatusError, *AssociationError, *TimeoutError, IsCanceled
dicomweb*StatusError, ErrResponseTooLarge, ErrPartTooLarge
volume*Error with a Kind
filesetErrNotDICOM, ErrNoFileMeta, ErrInvalidFileID, ErrTooManyRecords

Key types and functions

NameWhat it is for
DicomErrorThe interface every type here satisfies: Error, ErrorCode, Details.
DicomUnicodeDecodeErrorA character set that is unknown, or text that cannot be decoded or encoded.
EncapsulationErrorEncapsulated pixel data that cannot be parsed or split.
InvalidDicomError, InvalidVRError, InvalidValueErrorOther types with constructors, for code of your own.
DicomKeyError, DicomInvalidTagError, DicomBytesLengthErrorThe same.

Example

// Tell an encoding failure apart from other errors, and read its code.
package main

import (
	"context"
	stderrors "errors"
	"fmt"

	"github.com/amrshadid/go-dicom/charset"
	"github.com/amrshadid/go-dicom/config"
	dicomerrors "github.com/amrshadid/go-dicom/errors"
)

func main() {
	ctx := config.StrictReading(context.Background())
	_, err := charset.ConvertEncodingsWithContext(ctx, []string{"ISO_IR 999"})

	var decodeErr *dicomerrors.DicomUnicodeDecodeError
	if stderrors.As(err, &decodeErr) {
		fmt.Println("encoding:", decodeErr.Encoding)
		fmt.Println("code:", decodeErr.ErrorCode())
	}

	// Every type here satisfies DicomError.
	var de dicomerrors.DicomError
	if stderrors.As(err, &de) {
		fmt.Println("details:", de.Details())
	}
}

It prints encoding: ISO_IR 999, code: UNICODE_DECODE_ERROR and the details. The package is imported under another name so that the standard errors stays available.

Notes

  • The package name is errors, which shadows the standard library's. Import it as dicomerrors or similar.
  • The Execute…Hook functions return maps describing an error; nothing in the library calls them.

Note

Of the types here, only DicomUnicodeDecodeError and EncapsulationError are returned by the library itself. The others exist for callers who want the same shape for their own errors.

See also

charset · encaps · config · Configuration and errors

On this page