@@ -325,6 +325,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
325325 uint16_t seq = 0 ;
326326 uint16_t size = 0 ;
327327 uint8_t * data = NULL ;
328+ int handlerRc = 0 ;
328329
329330 if (server == NULL ) {
330331 return WH_ERROR_BADARGS ;
@@ -342,7 +343,7 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
342343 int rc = wh_CommServer_RecvRequest (server -> comm , & magic , & kind , & seq ,
343344 & size , data );
344345 /* Got a packet? */
345- if (rc == 0 ) {
346+ if (rc == WH_ERROR_OK ) {
346347 group = WH_MESSAGE_GROUP (kind );
347348 action = WH_MESSAGE_ACTION (kind );
348349 switch (group ) {
@@ -407,33 +408,56 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
407408#endif /* WOLFHSM_CFG_CERTIFICATE_MANAGER && !WOLFHSM_CFG_NO_CRYPTO */
408409
409410 default :
410- /* Unknown group. Return empty packet*/
411- /* TODO: Respond with aux error flag */
411+ /* Unknown group. Return empty packet */
412+ rc = WH_ERROR_NOTIMPL ;
413+ data = NULL ;
412414 size = 0 ;
413415 }
414416
415- /* Send a response */
416- /* TODO: Respond with ErrorResponse if handler returns an error */
417+ /* Capture handler result for logging. The response packet already
418+ * contains the error code for the client in the resp.rc field. */
419+ handlerRc = rc ;
420+
421+ /* Handle cancellation by modifying response kind */
417422#ifdef WOLFHSM_CFG_CANCEL_API
418- if (rc == WH_ERROR_CANCEL ) {
423+ if (handlerRc == WH_ERROR_CANCEL ) {
419424 /* notify the client that their request was canceled */
420425 kind = WH_MESSAGE_KIND (WH_MESSAGE_GROUP_CANCEL , 0 );
421426 size = 0 ;
422427 data = NULL ;
423- /* reset RC so the cancellation response is sent */
424- rc = 0 ;
425428 }
426429#endif
427- if (rc == 0 ) {
428- do {
429- rc = wh_CommServer_SendResponse (server -> comm , magic , kind , seq ,
430- size , data );
431- } while (rc == WH_ERROR_NOTREADY );
432- }
430+
431+ /* Always send the response to the client, regardless of handler error.
432+ * The response packet contains the operational error code for the
433+ * client in the resp.rc field. */
434+ do {
435+ rc = wh_CommServer_SendResponse (server -> comm , magic , kind , seq ,
436+ size , data );
437+ } while (rc == WH_ERROR_NOTREADY );
438+
439+ /* Log error code from request handler, if present */
440+ WH_LOG_ON_ERROR_F (& server -> log , WH_LOG_LEVEL_ERROR , handlerRc ,
441+ "Handler (group=%d, action=%d, seq=%d) returned %d" ,
442+ group , action , seq , handlerRc );
443+ (void )handlerRc ; /* suppress unused var warning */
444+
445+ /* Log error code from sending response, if present */
446+ WH_LOG_ON_ERROR_F (
447+ & server -> log , WH_LOG_LEVEL_ERROR , rc ,
448+ "SendResponse failed for (group=%d, action=%d, seq=%d): %d" , group ,
449+ action , seq , rc );
450+
451+ /* Handler errors are logged above via handlerRc but don't affect
452+ * return code. Errors from SendResponse are propagated back to the
453+ * caller in rc */
454+ }
455+ else if (rc != WH_ERROR_NOTREADY ) {
456+ /* Log error code from processing request, if present */
433457 WH_LOG_ON_ERROR_F (
434458 & server -> log , WH_LOG_LEVEL_ERROR , rc ,
435- "Request Handler for (group=%d, action=%d) Returned Error : %d" ,
436- group , action , rc );
459+ "RecvRequest failed for (group=%d, action=%d, seq=%d) : %d" , group ,
460+ action , seq , rc );
437461 }
438462
439463 return rc ;
0 commit comments