better error naming
This commit is contained in:
1 parent
f587a4389f
commit
cb7219a152
3 files changed
+21
-8
No files matched your search
@@ -14,7 +14,7 @@ from pdfmajor.lexer.token import (
|
||||
|
||||
from .state import ParsingState
|
||||
from .parsers import attempt_parse_prim, deal_with_collection_object
|
||||
from .exceptions import ParserError
|
||||
from .exceptions import InvalidKeywordPos, ParserError
|
||||
|
||||
from .objects.base import PDFObject
|
||||
from .objects.collections import PDFArray, PDFDictionary
|
||||
@@ -85,7 +85,7 @@ class PDFParser:
|
||||
if isinstance(last_ctx, IndirectObject):
|
||||
yield last_ctx.clone()
|
||||
elif self.strict:
|
||||
raise ParserError(f"Invalid position for 'endobj' {token}")
|
||||
raise InvalidKeywordPos(token)
|
||||
elif token.value == b"stream":
|
||||
last_ctx = self.state.current_context
|
||||
if last_ctx is not None and isinstance(last_ctx, IndirectObject):
|
||||
@@ -101,9 +101,9 @@ class PDFParser:
|
||||
elif token.value == b"endstream":
|
||||
stream = self.state.context_stack.pop()
|
||||
if not isinstance(stream, PDFStream):
|
||||
raise ParserError(f"Invalid position for 'endstream' {token}")
|
||||
raise InvalidKeywordPos(token)
|
||||
else:
|
||||
raise ParserError(f"Invalud Keyword {token}")
|
||||
raise InvalidKeywordPos(token)
|
||||
elif isinstance(token, TokenArray):
|
||||
for obj in self.state.flush_int_collections():
|
||||
yield obj
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from pdfmajor.lexer.token import TokenKeyword
|
||||
from pdfmajor.exceptions import PDFMajorException
|
||||
|
||||
|
||||
@@ -5,6 +6,18 @@ class ParserError(PDFMajorException):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidKeywordPos(ParserError):
|
||||
def __init__(self, token: TokenKeyword) -> None:
|
||||
self.token = token
|
||||
super().__init__(
|
||||
f"keyword '{token.value}' was found at {token.start_loc}:{token.end_loc}"
|
||||
)
|
||||
|
||||
|
||||
class InvalidIndirectObjAccess(ParserError):
|
||||
pass
|
||||
|
||||
|
||||
class DecodeFailed(ParserError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from typing import Dict, Tuple
|
||||
from ..exceptions import ParserError
|
||||
from ..exceptions import InvalidIndirectObjAccess, ParserError
|
||||
from .base import PDFContextualObject, PDFObject
|
||||
from .primitives import PDFNull
|
||||
|
||||
@@ -63,7 +63,7 @@ class IndirectObject(PDFContextualObject):
|
||||
|
||||
def save_object(self, obj: PDFObject):
|
||||
if self.__read_only:
|
||||
raise ParserError(
|
||||
raise InvalidIndirectObjAccess(
|
||||
f"attempted to override a read-only reference of an object {obj}"
|
||||
)
|
||||
self.__col.objs[(self.obj_num, self.gen_num)] = obj
|
||||
@@ -76,7 +76,7 @@ class IndirectObject(PDFContextualObject):
|
||||
|
||||
def pass_item(self, item: PDFObject):
|
||||
if self.__read_only:
|
||||
raise ParserError(
|
||||
raise InvalidIndirectObjAccess(
|
||||
f"attempted to add to an read-only reference of an object {item}"
|
||||
)
|
||||
cur_obj = self.get_object()
|
||||
@@ -85,4 +85,4 @@ class IndirectObject(PDFContextualObject):
|
||||
elif isinstance(cur_obj, PDFNull):
|
||||
self.save_object(item)
|
||||
else:
|
||||
raise ParserError(f"Invalid redefinition of an object {item}")
|
||||
raise InvalidIndirectObjAccess(f"Invalid redefinition of an object {item}")
|
||||
Reference in new issue
Block a user