[GIS] How to debug a geoprocessing service on the server

arcgis-servergeoprocessing

I've a geoprocessing script that I'm trying to get working. I've turned on Show Messages, so I can see how far in the script I've gotten before the submitted job fails, but it's not helping, as I'm getting errors that I don't understand exactly, because I don't know where they occurred. Is there any way to debug server geoprocessing scripts on the server?

EDIT:
Error is occurring in this code, which worked fine locally. I'm not getting either of the messages returned, so something seems to be going wrong with the layers.

for layer in mxdLayers:
    try:
        arcpy.AddMessage("Layer name is %s" % (layer.name))
        layer.visible = visibleLayers[layer.name]
    except:
        arcpy.AddMessage("Layer not set: " + layer.name)

Error message is Layer: Unexpected error

Edit 2:
The error is not in the code above. The following code takes the parameter and stores the passed in features in a scratch gdb. Currently this is passed as an empty string, as I haven't gotten to testing this part yet.

# Get the passed point graphics and save to scratch workspace
inPointFset = arcpy.GetParameterAsText(9)
if inPointFset == '#' or not inPointFset:
    inPointFset = None
else:
    arcpy.CopyFeatures_management(inPointFset, os.path.join(arcpy.env.scratchWorkspace, r"scratch.gdb\Points"))

This code then changes the data source of the first mxd layer (which is why it's blowing up; the first layer doesn't reference the points feature class in the scratch gdb), but since the parameter was an empty string, I'm not sure why this is evaluating to true.

mxdLayers = arcpy.mapping.ListLayers(mapDoc)
if inPointFset:
    arcpy.AddMessage("Points")
    mxdLayers[0].replaceDataSource(os.path.join(arcpy.env.scratchWorkspace, r"scratch.gdb"), "FILEGDB_WORKSPACE", "Points")

Best Answer

I typically use a simple error handling class:

import sys
import traceback
import inspect

class ErrorHandling(object):
    '''
    ErrorHandling Provides User/Developers with detailed information about
    errors that occur in the code.  By using this class, you can better
    debug a program or solve on site issues.
    '''


    def __init__(self):
        '''
        Constructor - no parameters
        '''
        pass

    def trace(self):
        '''
        trace finds the line, the filename and error message and returns it 
        to the user
        '''
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]
        # script name + line number
        line = tbinfo.split(", ")[1]
        filename = inspect.getfile( inspect.currentframe() )
        # Get Python syntax error
        #
        synerror = traceback.format_exc().splitlines()[-1]
        return line, filename, synerror

Then I reference it in a try except clause.

import arcpy, os 
import ErrorHandling as ErrorHandling
try:
            <your code>
except arcpy.ExecuteError:
            EH = ErrorHandling.ErrorHandling()
            line, filename, err = EH.trace()
            m = "Python error on " + line + " of " + __file__ + \
                " : with error - " + err
            arcpy.AddMessage(m)

This way if any of my code errors it will give me a line number and explanation of the error.

Related Question