#!/usr/bin/env python
#
# Copyright (C) 2013-2016 DNAnexus, Inc.
#
# This file is part of dx-toolkit (DNAnexus platform client libraries).
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may not
#   use this file except in compliance with the License. You may obtain a copy
#   of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import json
import argparse
import os

parser = argparse.ArgumentParser(description='Creates job_error.json in your home directory, a JSON file to include the error type and message for the running job.  There are two types of errors you may report: 1) AppError (the default) for recognized actionable errors, and 2) AppInternalError for unexpected application errors.')
parser.add_argument('message', help='Error message for the job')
parser.add_argument('type', choices=['AppInternalError', 'AppError'], help='Error type', nargs='?', default='AppError')
args = parser.parse_args()

value = None

error_hash = {
    "error": {
        "type": args.type,
        "message": args.message
        }
    }

with open(os.path.expanduser(os.path.join('~', 'job_error.json')), 'w') as error_file:
    error_file.write(json.dumps(error_hash, indent=4) + '\n')

parser.exit(1, 'Exiting with nonzero exit code to report error\n')
