Every exception of the library implements SubtitleToolbox\Exceptions\SubtitleToolboxException. One catch block handles all of them, and lets errors from other code pass.
use SubtitleToolbox\Exceptions\SubtitleToolboxException;
use SubtitleToolbox\Format;
use SubtitleToolbox\Subtitle;
try {
$vtt = Subtitle::fromString($upload, Format::SubRip)->toString(Format::WebVtt);
} catch (SubtitleToolboxException $e) {
return response($e->getMessage(), 422);
}| Exception | Extends | getCode() |
Thrown for |
|---|---|---|---|
ParsingException |
\RuntimeException |
100 | content that a parser or fromArray() cannot read, or a byte that is not valid in the source encoding |
UnsupportedFormatException |
\RuntimeException |
102 | a format that the library cannot read or write, see Unsupported formats |
InvalidArgumentException |
\InvalidArgumentException |
104 | an invalid argument or option, see Invalid arguments |
CueNotFoundException |
\RuntimeException |
105 | removeCue(), splitCue() or joinCues() with an index that has no cue |
UnknownFormatException |
UnsupportedFormatException |
106 | load() or fromString() without a format when detection finds no format |
OcrException |
\RuntimeException |
107 | an OCR engine that fails on one image. Tesseract exits with an error, or php-glyph-ocr cannot read the image |
UnwritableContentException |
\RuntimeException |
108 | toString() or save() with a subtitle that the output format cannot hold, see Unwritable content |
TranslationException |
\RuntimeException |
109 | a translation service that fails, see Translation errors |
- SPL classes: each class extends an SPL class, so
catch (\InvalidArgumentException $e)andcatch (\RuntimeException $e)also work. - Messages:
InvalidArgumentException,UnwritableContentExceptionandCueNotFoundExceptionkeep the plain message. The other classes start it with the class name and the code, for exampleParsingException (Error #100):. See compatibility.md for the parts of an exception that stay stable. - Previous exception: every constructor takes the message, then an optional
$previousexception.ParsingExceptiontakes the line number before it.getPrevious()returns it. - Final classes: every exception class is
final, exceptUnsupportedFormatException.UnknownFormatExceptionextends it. Do not extend it, see compatibility.md. - Line number:
ParsingException::getLineNumber()returns the 1-based input line when the parser knows it, and null otherwise. The message then ends with the line, for example(line 12). Read the line fromgetLineNumber(), not from the message.
Lenient mode skips a broken block in place of throwing.
UnsupportedFormatException covers these cases:
fromString()orload()with a format that the library cannot read, such asFormat::PlainText.toString()orsave()with a format that the library cannot write, such asFormat::VobSub.save()without a format and with an unknown extension, such asout.unknown.- An MKV, WebM, MP4 or MPEG-TS file in
load()orfromString()with a format. CallloadTrack()instead. - An MKV, WebM, MP4 or MPEG-TS file without exactly 1 subtitle track in
load()orfromString()without a format. - Detection that finds no format. This throws the subclass
UnknownFormatException.
InvalidArgumentException covers these cases:
- An invalid value, for example frame rate 0 or NAN in a number option.
- A retiming or
append()that moves a cue time to 100000 hours or more, for examplescale(1e300). - A missing MicroDVD output frame rate.
- 2 objects of one class in
ReadOptions::$formatsorWriteOptions::$formats, or a write class inReadOptionsand a read class inWriteOptions. - A missing OCR engine, a missing
tesseractprogram, a missing Tesseract language or a missing php-glyph-ocr package. - A missing
ext-curlfor the translation engines. - A
ReadOptions::$encodingwithout a PAC code page, such asKOI8-R, for a PAC file.
UnwritableContentException covers these cases:
- More than 4 lines or 32 characters per line in SCC.
- A character that CEA-608 cannot show in SCC, for example
日. - A cue without an image in PGS.
- An image cue without text in
toString()orsave()with a text format, unlessWriteOptions::$skipImageCuesis set. Run OCR first or write a bitmap format. - An image position or size above 65535 in PGS.
- A PNG in PGS output whose size differs from the size in the image data of its cue, for example a 2x2 PNG for a 720x80 image.
- A cue time after 13:15:21.858 in PGS, the limit of its 32-bit time stamps of 90 kHz.
- A subtitle number over 65535 in EBU STL.
- Cue text that needs more than 241 TTI blocks of 112 bytes in EBU STL.
- A subtitle number over 65535, or cue text of 65535 bytes or more, in PAC.
- A line of more than 51 bytes in Cavena 890.
- Text that is not UTF-8 in a JSON format.
- Text that is not UTF-8 in TTML, iTT or SAMI output.
- A stored TTML head that is not valid XML.
- An
AssWriteOptions::$stylefield that theFormat:line of the styles lacks. - A cue without words in Google Speech-to-Text output with speaker diarization.
TranslationException covers these cases:
- An HTTP error, such as 403 for a wrong key or 456 for a used-up DeepL quota.
- A request without a response.
- An answer that the engine cannot read.
These readers set the line number:
| Reader | Line |
|---|---|
| ASS and SSA, MicroDVD, MPSub, SubViewer, MPL2, TMPlayer, SCC | the line of the error |
| SubRip, SBV and WebVTT, also the stream readers | the line of the error. A message about a missing cue number or timing line quotes the line, cut to 60 characters |
| SAMI | the line of the SYNC tag |
| CSV and TSV | the line where the row starts, or where an unclosed quote opens |
| YouTube XML formats, such as srv3 and transcript XML | the line of the XML element |
| HTML transcripts | the line of the paragraph or time |
| FFmpeg metadata and OGM chapters | the line of the error |
OcrReplaceList::fromSubtitleEditXml() |
the line of the XML error |
ShotChanges::fromText(), SpeechReference::fromFfmpegSilencedetect() |
the line of the error |