# Generating a unique reference number for logging purposes in a custom exception

I've come up with a base class for exceptions for this SMS-based service I am working on. The basic idea behind the service is that a user sends a text message to a number, the message requests a particular action to be carried out by an agent that logs into a different website, from which some account information is scraped and spat back out to the user. Small take on QoL. Obviously, things don't always work as intended for different reasons.

When something goes wrong and the returned text message doesn't contain the requested information, I want the user to have a reference number to that error, should the issue persist. To accommodate for that I will keep logs, where I will quickly be able to look up what happened using the reference number. The logs will contain full error information.

Intended use of the custom exceptions:

try:
except AuthenticationError as e:


...

import logging
import uuid

logging.basicConfig(filename='errors.log', level=logging.ERROR)

class ServiceException(Exception):
def __init__(self, message, url):
self.reference = str(uuid.uuid4()).split('-')[0]
error_message = f'{message}: \'{url}\' | error ref: {self.reference}'
logging.error(error_message)
super(Exception, self).__init__(error_message)

class AuthenticationError(ServiceException):
"""Ambiguous error prevented successful authentication."""

• Do you have any particular concerns or something you want reviewers to look into? Mar 14 '18 at 16:09
• Entirely forgot to mention that! I would just like to know if I went about the code the right way. I suppose I would be concerned if cutting the UUID short will keep it unique, otherwise I'm pretty happy with how the exception works, but if there's another way, I'll be glad to hear it. @Coal_ Mar 14 '18 at 16:28