Internal Service Error - 500

khangminh031199

minh minh

Posted on August 5, 2024

Internal Service Error - 500

Hello everyone, I use FastAPI to write my application: APIGateWay.
Currently I'm having problems with my service, sometimes I get error 500 with client requests. Calling the same request twice gives an error. The third time it was successful.
I was also able to reproduce the error and set the log but the error returned has no description at all, it is an empty string. Anyone help me, please check my code.

@app.post('/ZNS/SendZNS', tags=['ZNS'])
async def sendv2ZNS(request_data: dict, current_user: User = Depends(get_current_active_user)):
try:
time.sleep(0.1)
filter_service = {'sign': 'zns'}
service = mydb.services.find_one(filter_service)
service_id = str(service.get('_id'))
api_key = current_user.get('secret_etelecom')
if api_key is None:
raise HTTPException(status_code=400,
detail={"msg": "Your account is not yet in the product system."})

    header = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {api_key}'
    }

    url = api.get_link_function("template", filter_service)
    template_id = str(request_data.get('template_id', '')).replace(' ', '')
    oa_id = request_data.get('oa_id', '')

    if oa_id is None:
        if str(oa_id) != str(current_user.get('oa_id')):
            raise HTTPException(status_code=400,
                                detail={"msg": "The OA ID you provided has not been registered in the system."})
    if template_id is not None and template_id != "":
        template_id = template_id
        payload = json.dumps({
            "oa_id": oa_id,
            "template_id": int(template_id)
        })
        async with httpx.AsyncClient() as client:
            response = await client.post(url, headers=header, data=payload)

        res_template = response.json()
        if response.status_code == 200:
            type = res_template.get('type')
            if type == 'otp':
                if api.get_zns_request(current_user.get('_id'), service_id, 'remaining_otp') == 0:
                    raise HTTPException(status_code=429, detail={"msg": "Too many requests"})
            elif type == 'text':
                if api.get_zns_request(current_user.get('_id'), service_id, 'remaining_text') == 0:
                    raise HTTPException(status_code=429, detail={"msg": "Too many requests"})
            elif type == 'table':
                if api.get_zns_request(current_user.get('_id'), service_id, 'remaining_table') == 0:
                    raise HTTPException(status_code=429, detail={"msg": "Too many requests"})
            else:
                if api.get_zns_request(current_user.get('_id'), service_id, 'remaining_rating') == 0:
                    raise HTTPException(status_code=429, detail={"msg": "Too many requests"})

    url = api.get_link_function("sendZNS", filter_service)
    url_gw = BASE_URL + '/ZNS/SendZNS'
    payload = json.dumps(request_data)
    async with httpx.AsyncClient() as client:
        response = await client.post(url, headers=header, data=payload)
    result = response.json()

    if response.status_code == 200:
        if type == 'otp':
            api.decrease_remaining_zns_request(current_user.get('_id'), service_id, 'remaining_otp')
        elif type == 'text':
            api.decrease_remaining_zns_request(current_user.get('_id'), service_id, 'remaining_text')
        elif type == 'table':
            api.decrease_remaining_zns_request(current_user.get('_id'), service_id, 'remaining_table')
        else:
            mydb.rating_logs.insert_one(result)

    api.save_log(current_user.get('_id'), current_user.get('name'), 'ZNS', getNOW(), url, request_data, result, "",
                 "", url_gw)
    LOGGER.error(
        f"ITS/ZNS main HTTP: Time: {datetime.now()} - status_code: {response.status_code} - detail: {result}")
    return result
except HTTPException as e:
    LOGGER.error(f"ITS/ZNS HTTPException: time: {datetime.now()} - detail: {e.detail} - e: {e}")
    return JSONResponse(content=e.detail, status_code=200)
except Exception as e:
    api.save_log(current_user.get('_id'), current_user.get('name'), 'ZNS', getNOW(), "ERROR", request_data, str(e),
                 "", "", "url_gw")
    LOGGER.error(f"ITS/ZNS Exception: {e}")
    raise HTTPException(status_code=500, detail={"error": "Internal Server Error"})
Enter fullscreen mode Exit fullscreen mode
đź’– đź’Ş đź™… đźš©
khangminh031199
minh minh

Posted on August 5, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related