{"record":{"id":"e296057a67634d99","repo":"iflytek/astron-agent","slug":"failed-to-get-current-user-id-using-system-as-creator","errorCode":null,"errorMessage":"Failed to get current user ID, using system as creator","messagePattern":"Failed to get current user ID, using system as creator","errorType":"console","errorClass":null,"httpStatus":null,"severity":"info","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/service/notification/impl/NotificationServiceImpl.java","lineNumber":234,"sourceCode":"        Notification notification = new Notification();\n\n        // Manually copy properties, excluding type field\n        notification.setTitle(request.getTitle());\n        notification.setBody(request.getBody());\n        notification.setTemplateCode(request.getTemplateCode());\n        notification.setPayload(request.getPayload());\n        notification.setExpireAt(request.getExpireAt());\n        notification.setMeta(request.getMeta());\n\n        // Set type as String type code\n        notification.setType(type.getCode());\n\n        // Get current operating user\n        try {\n            String currentUid = RequestContextUtil.getUID();\n            notification.setCreatorUid(currentUid);\n        } catch (Exception e) {\n            log.warn(\"Failed to get current user ID, using system as creator\");\n        }\n\n        return notification;\n    }\n\n    private NotificationPageResponse getUserNotificationsByUid(String receiverUid, NotificationQueryRequest queryRequest) {\n        if (receiverUid == null) {\n            throw new BusinessException(ResponseEnum.PARAMETER_ERROR);\n        }\n\n        List<NotificationDto> notifications;\n        long unreadCount = notificationDataService.countUserUnreadNotifications(receiverUid);\n        long totalCount;\n\n        if (Boolean.TRUE.equals(queryRequest.getUnreadOnly())) {\n            // Query unread messages only\n            notifications = notificationDataService.getUserUnreadNotifications(\n                    receiverUid, queryRequest);","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/service/notification/impl/NotificationServiceImpl.java#L216-L252","documentation":"NotificationServiceImpl.createNotificationEntity tries to obtain the current operator's UID via RequestContextUtil.getUID() to set creatorUid. If that call throws (no request context, unauthenticated, or context cleared), the code catches Exception, logs \"Failed to get current user ID, using system as creator\", and falls back to leaving creatorUid unset (system). This is a logged warning fallback, not a thrown error.","triggerScenarios":"notification() called from a path without an HTTP request context (Kafka consumer, scheduled job, async thread) or before authentication is established, so RequestContextUtil.getUID() throws.","commonSituations":"System-generated notifications (billing events, background tasks) with no logged-in user; async executor thread lacking the request-context ThreadLocal propagated; internal API call without auth headers.","solutions":["If a user context should exist, verify auth middleware/interceptor ran and propagated the UID.","For async processing, propagate the request context (or UID explicitly) to the worker thread before calling notification().","Accept the fallback only for genuinely system-initiated notifications; otherwise pass the creator UID as an explicit parameter.","Consider upgrading the log to include the notification type/reason to aid tracing."],"exampleFix":"// before\nString currentUid = RequestContextUtil.getUID();\nnotification.setCreatorUid(currentUid);\n// after\nString currentUid = RequestContextUtil.getUIDSafe(); // returns null if absent\nnotification.setCreatorUid(currentUid != null ? currentUid : SYSTEM_UID);","handlingStrategy":"fallback","validationCode":"// caller-side: pass the UID explicitly when available\nnotificationService.notification(req, currentUser != null ? currentUser.getUid() : null);","typeGuard":"boolean hasUserContext = RequestContextUtil.getUIDOrNull() != null;","tryCatchPattern":"try {\n    String uid = RequestContextUtil.getUID();\n    notification.setCreatorUid(uid);\n} catch (Exception e) {\n    log.warn(\"Failed to get current user ID, using system as creator, source={}\", trigger); \n}","preventionTips":["Propagate request context ThreadLocals to async/consumer threads.","For system-initiated notifications, pass an explicit SYSTEM creator instead of relying on the fallback.","Only call getUID() where an authenticated request is guaranteed.","Tag these warnings with the notification type to trace missing-context paths."],"tags":["request-context","fallback","notifications","authentication"],"backgroundTag":"missing-credentials","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"}