moodle/moodle · error · InvalidWidthException

InvalidWidthException

Error message

InvalidWidthException (invalid border part width: %s)

What it means

BorderPart validates that the width is one of the allowed widths (Border::WIDTH_THIN, _MEDIUM, _THICK) and throws InvalidWidthException otherwise; arbitrary widths would not map to valid XLSX border weights.

Solutions

  1. Use Border::WIDTH_THIN, Border::WIDTH_MEDIUM or Border::WIDTH_THICK constants only.
  2. Clamp/map desired visual widths to the nearest allowed constant (thin/medium/thick).
  3. Validate config-supplied width strings against BorderPart::$allowedWidths before construction.

Example fix

// before
$part = new BorderPart(Border::LEFT, Color::BLACK, '2px');
// after
$part = new BorderPart(Border::LEFT, Color::BLACK, Border::WIDTH_THICK);
Defensive patterns

Strategy: validation

Validate before calling

// assert in_array($width, BorderPart::$allowedWidths, true) before constructing
if (!in_array($width, \OpenSpout\Common\Entity\Style\BorderPart::$allowedWidths, true)) {
    throw new InvalidArgumentException('Unsupported border width: '.$width);
}

Type guard

function isValidBorderWidth(?string $width): bool {
    return $width !== null && in_array($width, \OpenSpout\Common\Entity\Style\BorderPart::$allowedWidths, true);
}

Try / catch

try {
    $part = new BorderPart($name, $color, $width, $style);
} catch (OpenSpout\Common\Exception\Style\InvalidWidthException $e) {
    // clamp to nearest of thin/medium/thick and retry
}

Prevention

When it happens

Trigger: new BorderPart(Border::LEFT, Color::BLACK, '2px', ...) or any $width string not in BorderPart::$allowedWidths when building a border part.

Common situations: Passing pixel or point values ('1px','0.5pt') instead of the named constants; config-driven styling where widths come from user input; migrating code from another spreadsheet library that uses numeric widths.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/0166c337dae85c04. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/openspout/src/Common/Entity/Style/BorderPart.php:62

     *
     * @throws InvalidNameException
     * @throws InvalidStyleException
     * @throws InvalidWidthException
     */
    public function __construct(
        string $name,
        string $color = Color::BLACK,
        string $width = Border::WIDTH_MEDIUM,
        string $style = Border::STYLE_SOLID
    ) {
        if (!\in_array($name, self::allowedNames, true)) {
            throw new InvalidNameException($name);
        }
        if (!\in_array($style, self::allowedStyles, true)) {
            throw new InvalidStyleException($style);
        }
        if (!\in_array($width, self::allowedWidths, true)) {
            throw new InvalidWidthException($width);
        }

        $this->name = $name;
        $this->color = $color;
        $this->width = $width;
        $this->style = $style;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getStyle(): string
    {
        return $this->style;
    }

View on GitHub (pinned to 48ea8c3322)