adding intial exceptions to message dir, replaced locklainns exceptions with custom ones

This commit is contained in:
enus.linden
2008-10-01 08:42:11 +00:00
committed by Salad Dais
parent 1973924b4e
commit d197a7ac6e
10 changed files with 109 additions and 33 deletions

View File

@@ -235,12 +235,12 @@ class LLSDDeserializer(grok.GlobalUtility):
>>> llsd = deserializer.deserialize_string('mumpitz') # this is not LLSD
Traceback (most recent call last):
...
DeserializationFailed: deserialization failed for 'mumpitz', reason: invalid token at index 0: 109
DeserializationFailed: deserialization failed for 'mumpitz', reason: 'invalid token at index 0: 109'
>>> llsd = deserializer.deserialize_string('barfoo')
Traceback (most recent call last):
...
DeserializationFailed: deserialization failed for 'barfoo', reason: binary notation not yet supported
DeserializationFailed: deserialization failed for 'barfoo', reason: 'binary notation not yet supported'
"""

View File

@@ -86,7 +86,7 @@ class DeserializerNotFound(DeserializationError):
self.content_type = content_type
def __str__(self):
return "deserialization for %s not supported" %self.content_type
return "deserialization for '%s' not supported" % (self.content_type)
class CredentialDeserializerNotFound(DeserializationError):
"""raised if a deserializer for a certain content type couldn't be found
@@ -112,15 +112,91 @@ class DeserializationFailed(DeserializationError):
self.reason = reason
def __str__(self):
return "deserialization failed for '%s', reason: %s" %(self.payload, self.reason)
return "deserialization failed for '%s', reason: '%s'" %(self.payload, self.reason)
### Message System related errors
class MessageSystemError(Error):
"""message system related exception"""
class MessageTemplateNotFound(MessageSystemError):
""" message template file not found
stores the context in a ``context`` attribute
"""
def __init__(self, context='',template=''):
self.context = context
self.template = template
def __str__(self):
return "No message template found, context: '%s'" % (self.context)
class MessageTemplateParsingError(MessageSystemError):
""" message template parsing error
stores the context in a ``context`` attribute
"""
def __init__(self, context='',template=''):
self.context = context
self.template = template
def __str__(self):
return "Error parsing message template, context: '%s'" % (self.context)
class CircuitNotFound(MessageSystemError):
""" circuit to host could not be found
stores the host missing a circuit in a ``host`` attribute
"""
def __init__(self, host='', reason=''):
self.host = host
self.reason = reason
def __str__(self):
return "No circuit to '%s' found, reason: '%s'" % (self.host, self.reason)
class MessageBuildingError(MessageSystemError):
""" problem serializing packet data
stores the label and reason in ``label`` and ``reason`` attributes
"""
def __init__(self, label='', reason=''):
self.label = label
self.reason = reason
def __str__(self):
return "Error serializing '%s' due to reason: '%s'" % (self.label, self.reason)
class MessageSerializationError(MessageSystemError):
""" problem serializing packet data
stores the label and reason in ``label`` and ``reason`` attributes
"""
def __init__(self, label='', reason=''):
self.label = label
self.reason = reason
def __str__(self):
return "Error serializing '%s' due to reason: '%s'" % (self.label, self.reason)
class MessageDeserializationError(MessageSystemError):
""" problem deserializing packet data
stores the label and reason in ``label`` and ``reason`` attributes
"""
def __init__(self, label='', reason=''):
self.label = label
self.reason = reason
def __str__(self):
return "Error serializing '%s' due to reason: '%s'" % (self.label, self.reason)
##########################
### high level exceptions
@@ -156,7 +232,6 @@ class UserNotAuthorized(AgentDomainError):
class UserRezFailed(AgentDomainError):
"""an error raised in case a user couldn't rez on a sim
stores the credentials used inside a ``credentials`` attribute
stores the region used inside a ``region`` attribute

View File

@@ -16,8 +16,10 @@ or in
http://svn.secondlife.com/svn/linden/projects/2008/pyogp/LICENSE.txt
$/LicenseInfo$
"""
from pyogp.lib.base import exc
class MessageFactory(object):
#not here, this goes somewhere else
@@ -64,11 +66,12 @@ class MessageFactory(object):
else:
#make sure it isn't SINGLE and trying to create a new block
if template_block.type == MsgBlockType.MBT_SINGLE:
raise Exception('ERROR: can"t have more than 1 block when its supposed to be 1')
raise exc.MessageBuildingError("block count", "exceding intended block count of 1")
elif template_block.type == MsgBlockType.MBT_MULTIPLE and \
template_block.number == block_data.block_number:
raise Exception('ERROR: we are about to exceed block total')
raise exc.MessageBuildingError("block count", "exceeding intended block count")
block_data.block_number += 1
self.current_block = MsgBlockData(block_name)
@@ -84,14 +87,14 @@ class MessageFactory(object):
what type (and therefore size) of the data that is being passed in. """
self.has_been_built = False
if self.current_template == None:
raise Exception('Attempting to add data to a null message')
raise exc.MessageBuildingError("data", "invalid context")
if self.current_block == None:
raise Exception('Attempting to add data to a null block')
raise exc.MessageBuildingError("data", "adding to what should be a null block")
template_variable = self.current_template.get_block(self.cur_block_name).get_variable(var_name)
if template_variable == None:
raise Exception('Variable is not in the block')
raise exc.MessageBuildingError("data", "variable is not appropriate for block")
#this should be the size of the actual data
size = sizeof(data_type)
@@ -110,7 +113,7 @@ class MessageFactory(object):
else:
#size check can't be done on VARIABLE sized variables
if self.__check_size(var_name, data, size) == False:
raise Exception('Variable size isn"t the same as the template size')
raise exc.MessageBuildingError("data", "invalid variable size")
self.current_block.add_data(var_name, data, size)

View File

@@ -1,6 +1,6 @@
#!/usr/bin/python
"""
@file packet.py
@file template.py
@author Linden Lab
@date 2008-06-13
@brief Iniitializes path directories

View File

@@ -28,6 +28,7 @@ from template_parser import MessageTemplateParser
from data_packer import DataPacker
from types import MsgType, EndianType
from pyogp.lib.base import exc
class TemplateDictionary(GlobalUtility):
"""the dictionary with all known templates"""

View File

@@ -36,7 +36,7 @@ class MessageTemplateParser(object):
"""
def __init__(self, template_file):
if template_file == None:
raise Exception('Template file cannot be None')
raise exc.MessageTemplateNotFound("initializing template parser")
self.template_file = template_file
self.message_templates = []
@@ -216,7 +216,7 @@ class MessageTemplateParser(object):
var_size = int(parts[2])
if var_size <= 0:
raise Exception('Bad variable size')
raise exc.MessageTemplateParsingError("variable size %s does not match %s" % (var_size, type_string))
#if the size hasn't been read yet, then read it from message_types
if var_size == -1:
var_size = sizeof(var_type)

View File

@@ -32,6 +32,8 @@ from types import MsgType, MsgBlockType, MsgFrequency, PacketLayout, EndianType,
from data_unpacker import DataUnpacker
from interfaces import IUDPPacket
from pyogp.lib.base import exc
class UDPPacketDeserializer(grok.Adapter):
grok.implements(IDeserialization)
grok.context(str)
@@ -60,9 +62,7 @@ class UDPPacketDeserializer(grok.Adapter):
""" Determines the template that the message in the buffer
appears to be using. """
if PacketLayout.PACKET_ID_LENGTH >= len(message_buffer):
raise Exception("Reading " + str(PacketLayout.PACKET_ID_LENGTH) + \
" bytes from a buffer that is only " + \
str(len(message_buffer)) + " bytes long")
raise exc.MessageDeserializationError("packet length", "template mismatch")
header = message_buffer[PacketLayout.PACKET_ID_LENGTH:]
self.current_template = self.__decode_header(header)
@@ -119,7 +119,7 @@ class UDPPacketDeserializer(grok.Adapter):
def __decode_data(self, data):
if self.current_template == None:
raise Exception('Attempting to decode data without validating it')
raise exc.MessageTemplateNotFound("deserializing data")
msg_data = MsgData(self.current_template.name)
@@ -234,8 +234,7 @@ class UDPPacketDeserializer(grok.Adapter):
decode_pos)
else:
raise Exception('Attempting to read variable with unknown size \
of ' + str(data_size))
raise exc.MessageDeserializationError("variable", "unknown data size")
decode_pos += data_size
@@ -255,7 +254,7 @@ class UDPPacketDeserializer(grok.Adapter):
if len(msg_data.blocks) <= 0 and len(self.current_template.blocks) > 0:
raise Exception("Read message is empty")
raise exc.MessageDeserializationError("message", "message is empty")
packet.message_data = msg_data
return packet

View File

@@ -30,6 +30,7 @@ from data_packer import DataPacker
from pyogp.lib.base.interfaces import ISerialization, IDeserialization
from interfaces import IUDPDispatcher, IUDPPacket
from message import Message, Block
from pyogp.lib.base import exc
#maybe make a global utility
class UDPDispatcher(object):
@@ -76,7 +77,7 @@ class UDPDispatcher(object):
#host = self.udp_client.get_sender()
circuit = self.find_circuit(host)
if circuit == None:
raise Exception("No circuit found")
raise exc.CircuitNotFound(host, 'preparing to check for packets')
#Case - trusted packets can only come in over trusted circuits
if circuit.is_trusted and \

View File

@@ -31,6 +31,7 @@ from types import MsgType, MsgBlockType, MsgFrequency, EndianType, sizeof
from data_packer import DataPacker
from pyogp.lib.base.interfaces import ISerialization
from pyogp.lib.base import exc
class UDPPacketSerializer(grok.Adapter):
""" an adpater for serializing a IUDPPacket into the UDP message format
@@ -105,9 +106,7 @@ class UDPPacketSerializer(grok.Adapter):
#receieve this know how many to read automatically
if template_block.type == MsgBlockType.MBT_MULTIPLE:
if template_block.number != block_count:
raise Exception('Block ' + template_block.name + ' is type MBT_MULTIPLE \
but only has data stored for ' + str(block_count) + ' out of its ' + \
template_block.number + ' blocks')
raise exc.MessageSerializationError(template_block.name, "block data mismatch")
#variable means the block variables can repeat, so we have to
#mark how many blocks there are of this type that repeat, stored in
@@ -125,9 +124,8 @@ class UDPPacketSerializer(grok.Adapter):
var_data = variable.data
if variable == None:
raise Exception('Variable ' + variable.name + ' in block ' + \
message_block.name + ' of message ' + message_data.name + \
" wasn't set prior to buildMessage call")
raise exc.MessageSerializationError(variable.name, "variable value is not set")
#if its a VARIABLE type, we have to write in the size of the data
if v.type == MsgType.MVT_VARIABLE:
@@ -142,8 +140,7 @@ class UDPPacketSerializer(grok.Adapter):
block_buffer += self.packer.pack_data(len(var_data), MsgType.MVT_U32)
#block_buffer += struct.pack('>I', var_size)
else:
raise Exception('Attempting to build variable with unknown size \
of ' + str(var_size))
raise exc.MessageSerializationError("variable size", "unrecognized variable size")
bytes += var_size

View File

@@ -79,14 +79,14 @@ In this case it should not find a deserializer and say so::
>>> cap = seed.get(['some_capability'])
Traceback (most recent call last):
...
DeserializerNotFound: deserialization for text/foobar not supported
DeserializerNotFound: deserialization for 'text/foobar' not supported
Try the same for GET:
>>> cap = Capability('test','http://127.0.0.1:12345/cap_wrong_content_type')
>>> cap.GET()
Traceback (most recent call last):
...
DeserializerNotFound: deserialization for text/foobar not supported
DeserializerNotFound: deserialization for 'text/foobar' not supported