
import json
import webapp2

from dashboard.pinpoint.models import job as job_module


class JobHandler(webapp2.RequestHandler):

  def post(self):
    job_id = self.request.get('job_id')

    # Validate parameters.
    try:
      job = job_module.JobFromId(job_id)
      self.response.write(json.dumps({'data': job.AsDict()}))
    except:  # pylint: disable=bare-except
      # There's no narrower exception we can catch. Catching
      # google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
      # doesn't appear to work here.
      # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
      self.response.write(json.dumps({'error': 'Unknown job id.'}))
      return

    del job


import json
import webapp2

from dashboard.pinpoint.models import job as job_module


class JobHandler(webapp2.RequestHandler):

  def post(self):
    job_id = self.request.get('job_id')

    # Validate parameters.
    try:
      job = job_module.JobFromId(job_id)
    except Exception as e:  # pylint: disable=broad-except
      # Catching google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
      # directly doesn't work.
      # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
      if e.__class__.__name__ == 'ProtocolBufferDecodeError':
        self.response.write(json.dumps({'error': 'Unknown job id.'}))
        return
      raise

    self.response.write(json.dumps({'data': job.AsDict()}))

Move Job handler out of exception block.
[pinpoint] Move Job handler out of exception block.

The exception block is solely used for Job loading exceptions.

Review-Url: https://codereview.chromium.org/2768293003

  
  import json
  import webapp2
  
  from dashboard.pinpoint.models import job as job_module
  
  
  class JobHandler(webapp2.RequestHandler):
  
    def post(self):
      job_id = self.request.get('job_id')
  
      # Validate parameters.
      try:
        job = job_module.JobFromId(job_id)
-       self.response.write(json.dumps({'data': job.AsDict()}))
-     except:  # pylint: disable=bare-except
+     except Exception as e:  # pylint: disable=broad-except
-       # There's no narrower exception we can catch. Catching
-       # google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
+       # Catching google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
-       # doesn't appear to work here.
+       # directly doesn't work.
        # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
+       if e.__class__.__name__ == 'ProtocolBufferDecodeError':
-       self.response.write(json.dumps({'error': 'Unknown job id.'}))
+         self.response.write(json.dumps({'error': 'Unknown job id.'}))
-       return
+         return
+       raise
  
-     del job
+     self.response.write(json.dumps({'data': job.AsDict()}))
  
Move Job handler out of exception block.
## Code Before:

import json
import webapp2

from dashboard.pinpoint.models import job as job_module


class JobHandler(webapp2.RequestHandler):

  def post(self):
    job_id = self.request.get('job_id')

    # Validate parameters.
    try:
      job = job_module.JobFromId(job_id)
      self.response.write(json.dumps({'data': job.AsDict()}))
    except:  # pylint: disable=bare-except
      # There's no narrower exception we can catch. Catching
      # google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
      # doesn't appear to work here.
      # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
      self.response.write(json.dumps({'error': 'Unknown job id.'}))
      return

    del job

## Instruction:
Move Job handler out of exception block.
## Code After:

import json
import webapp2

from dashboard.pinpoint.models import job as job_module


class JobHandler(webapp2.RequestHandler):

  def post(self):
    job_id = self.request.get('job_id')

    # Validate parameters.
    try:
      job = job_module.JobFromId(job_id)
    except Exception as e:  # pylint: disable=broad-except
      # Catching google.net.proto.ProtocolBuffer.ProtocolBufferDecodeError
      # directly doesn't work.
      # https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
      if e.__class__.__name__ == 'ProtocolBufferDecodeError':
        self.response.write(json.dumps({'error': 'Unknown job id.'}))
        return
      raise

    self.response.write(json.dumps({'data': job.AsDict()}))
