{"record":{"id":"06aabf19bf399ead","repo":"iflytek/astron-agent","slug":"e-message-customexception-errorresponse","errorCode":null,"errorMessage":"${e.message} (CustomException ErrorResponse)","messagePattern":"(.+?) \\(CustomException ErrorResponse\\)","errorType":"http","errorClass":"CustomException","httpStatus":null,"severity":"error","filePath":"core/knowledge/api/v1/api.py","lineNumber":150,"sourceCode":"\n    except ProtocolParamException as e:\n        error_msg = f\"{operation_callable.__name__} ProtocolParamException, reason {e}\"\n        logger.error(error_msg)\n        span_context.record_exception(e)\n        metric.in_error_count(code=CodeEnum.ParameterCheckException.code)\n        return ErrorResponse(code_enum=CodeEnum.ParameterCheckException, message=str(e))\n\n    except ThirdPartyException as e:\n        error_msg = f\"{operation_callable.__name__} err (ThirdParty), reason {e}\"\n        logger.error(error_msg)\n        span_context.record_exception(e)\n        metric.in_error_count(code=e.code)\n        # Create a CodeEnum-like object for the response\n        error_code = type(\"ErrorCode\", (), {\"code\": e.code, \"msg\": e.message})()\n        return ErrorResponse(code_enum=error_code, message=e.message)\n\n    except CustomException as e:\n        error_msg = f\"{operation_callable.__name__} err (Custom), reason {e}\"\n        logger.error(error_msg)\n        span_context.record_exception(e)\n        metric.in_error_count(code=e.code)\n        # Create a CodeEnum-like object for the response\n        error_code = type(\"ErrorCode\", (), {\"code\": e.code, \"msg\": e.message})()\n        return ErrorResponse(code_enum=error_code, message=e.message)\n\n    except Exception as e:  # pylint: disable=W0718\n        # Intentionally catch all exceptions here as part of global exception handling\n        error_msg = f\"{operation_callable.__name__} err (Unexpected), reason {e}\"\n        logger.error(error_msg)\n        span_context.record_exception(e)\n        metric.in_error_count(code=CodeEnum.ServiceException.code)\n        return ErrorResponse(\n            code_enum=CodeEnum.ServiceException,\n            message=f\"Internal server error:{error_msg}\",\n        )\n","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/api/v1/api.py#L132-L168","documentation":"The same handle_rag_operation wrapper also catches CustomException, the service's own business-logic exception. It logs, records the span, increments metrics with e.code, synthesizes a CodeEnum-like object, and returns ErrorResponse with the business error code and message. Unlike ThirdPartyException this signals a domain-rule violation inside the knowledge service rather than an upstream dependency failure.","triggerScenarios":"RAG API calls where business rules reject the request: invalid chunk indices in chunk_update/chunk_delete, nonexistent file/knowledge-base IDs, quota or state violations raised as CustomException by the service layer.","commonSituations":"Client operating on a deleted document; chunk_query with an out-of-range offset; domain validators rejecting payloads that pass schema validation.","solutions":["Read e.message to identify which business rule failed","Validate the target resource exists before calling the API (correct file/chunk/knowledge-base IDs)","Fix the request payload to satisfy the domain constraint","If the rule itself is wrong, adjust the service-layer validation that raises CustomException"],"exampleFix":"// before\ndelete_chunk(chunk_id)  # may raise CustomException\n// after\nif not chunk_exists(chunk_id):\n    raise CustomException(code=CodeEnum.DataNotFoundException.code, message=f\"chunk {chunk_id} not found\")","handlingStrategy":"validation","validationCode":"def validate_chunk_request(file_id, chunk_ids):\n    if not file_exists(file_id):\n        raise ValueError(f\"file {file_id} does not exist\")\n    if any(not chunk_exists(c) for c in chunk_ids):\n        raise ValueError(\"one or more chunks do not exist\")","typeGuard":"def is_business_error(resp: dict) -> bool:\n    return isinstance(resp, dict) and resp.get(\"code\", 0) != 0 and resp.get(\"code\") not in SYSTEM_ERROR_CODES","tryCatchPattern":"try:\n    resp = rag_api.chunk_update(payload)\nexcept CustomException as e:\n    log.info(\"business rule rejected: code=%s msg=%s\", e.code, e.message)\n    resp = None  # surface e.message to the end user as validation feedback","preventionTips":["Resolve resource IDs (files, chunks, KBs) through the list/query APIs before mutating them","Keep domain validation messages user-actionable","Sync client-side validation rules with server-side CustomException rules","Write tests asserting CustomException codes for known-invalid requests"],"tags":["python","fastapi","business-logic","rag","error-response"],"backgroundTag":"schema-validation-failed","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}