@@ -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,44 @@ 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 ;
412413 size = 0 ;
413414 }
414415
415- /* Send a response */
416- /* TODO: Respond with ErrorResponse if handler returns an error */
416+ /* Capture handler result for logging. The response packet already
417+ * contains the error code for the client in the resp.rc field. */
418+ handlerRc = rc ;
419+
420+ /* Handle cancellation by modifying response kind */
417421#ifdef WOLFHSM_CFG_CANCEL_API
418- if (rc == WH_ERROR_CANCEL ) {
422+ if (handlerRc == WH_ERROR_CANCEL ) {
419423 /* notify the client that their request was canceled */
420424 kind = WH_MESSAGE_KIND (WH_MESSAGE_GROUP_CANCEL , 0 );
421425 size = 0 ;
422426 data = NULL ;
423- /* reset RC so the cancellation response is sent */
424- rc = 0 ;
425427 }
426428#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- }
433- WH_LOG_ON_ERROR_F (
434- & server -> log , WH_LOG_LEVEL_ERROR , rc ,
435- "Request Handler for (group=%d, action=%d) Returned Error: %d" ,
436- group , action , rc );
429+
430+ /* Always send the response to the client, regardless of handler error.
431+ * The response packet contains the operational error code for the
432+ * client in the resp.rc field. */
433+ do {
434+ rc = wh_CommServer_SendResponse (server -> comm , magic , kind , seq ,
435+ size , data );
436+ } while (rc == WH_ERROR_NOTREADY );
437+
438+ /* Log any communication errors */
439+ WH_LOG_ON_ERROR_F (& server -> log , WH_LOG_LEVEL_ERROR , rc ,
440+ "SendResponse failed for (group=%d, action=%d): %d" ,
441+ group , action , rc );
442+
443+ (void )handlerRc ; /* Suppress unused variable warning until logging is
444+ * implemented */
445+
446+ /* Always return success when we processed a request, so no handler
447+ * error can terminate the server's request processing loop. */
448+ rc = WH_ERROR_OK ;
437449 }
438450
439451 return rc ;
0 commit comments