Skip to content

Commit e49dd12

Browse files
committed
feat(super-magic-module): add token usage tracking and event handling for super agent
1 parent 5e0d758 commit e49dd12

29 files changed

+2755
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
8+
namespace App\ErrorCode;
9+
10+
use App\Infrastructure\Core\Exception\Annotation\ErrorMessage;
11+
12+
/**
13+
* Event error codes range: 6000-6999
14+
* Used for event delivery and consumption related errors.
15+
*/
16+
enum EventErrorCode: int
17+
{
18+
// Event delivery errors
19+
#[ErrorMessage('event.delivery_failed')]
20+
case EVENT_DELIVERY_FAILED = 6000;
21+
22+
#[ErrorMessage('event.publisher_not_found')]
23+
case EVENT_PUBLISHER_NOT_FOUND = 6001;
24+
25+
#[ErrorMessage('event.exchange_not_found')]
26+
case EVENT_EXCHANGE_NOT_FOUND = 6002;
27+
28+
#[ErrorMessage('event.routing_key_invalid')]
29+
case EVENT_ROUTING_KEY_INVALID = 6003;
30+
31+
// Event consumption errors
32+
#[ErrorMessage('event.consumer_execution_failed')]
33+
case EVENT_CONSUMER_EXECUTION_FAILED = 6100;
34+
35+
#[ErrorMessage('event.consumer_not_found')]
36+
case EVENT_CONSUMER_NOT_FOUND = 6101;
37+
38+
#[ErrorMessage('event.consumer_timeout')]
39+
case EVENT_CONSUMER_TIMEOUT = 6102;
40+
41+
#[ErrorMessage('event.consumer_retry_exceeded')]
42+
case EVENT_CONSUMER_RETRY_EXCEEDED = 6103;
43+
44+
#[ErrorMessage('event.consumer_validation_failed')]
45+
case EVENT_CONSUMER_VALIDATION_FAILED = 6104;
46+
47+
// Event data errors
48+
#[ErrorMessage('event.data_serialization_failed')]
49+
case EVENT_DATA_SERIALIZATION_FAILED = 6200;
50+
51+
#[ErrorMessage('event.data_deserialization_failed')]
52+
case EVENT_DATA_DESERIALIZATION_FAILED = 6201;
53+
54+
#[ErrorMessage('event.data_validation_failed')]
55+
case EVENT_DATA_VALIDATION_FAILED = 6202;
56+
57+
#[ErrorMessage('event.data_format_invalid')]
58+
case EVENT_DATA_FORMAT_INVALID = 6203;
59+
60+
// Event queue errors
61+
#[ErrorMessage('event.queue_connection_failed')]
62+
case EVENT_QUEUE_CONNECTION_FAILED = 6300;
63+
64+
#[ErrorMessage('event.queue_not_found')]
65+
case EVENT_QUEUE_NOT_FOUND = 6301;
66+
67+
#[ErrorMessage('event.queue_full')]
68+
case EVENT_QUEUE_FULL = 6302;
69+
70+
#[ErrorMessage('event.queue_permission_denied')]
71+
case EVENT_QUEUE_PERMISSION_DENIED = 6303;
72+
73+
// Event processing errors
74+
#[ErrorMessage('event.processing_interrupted')]
75+
case EVENT_PROCESSING_INTERRUPTED = 6400;
76+
77+
#[ErrorMessage('event.processing_deadlock')]
78+
case EVENT_PROCESSING_DEADLOCK = 6401;
79+
80+
#[ErrorMessage('event.processing_resource_exhausted')]
81+
case EVENT_PROCESSING_RESOURCE_EXHAUSTED = 6402;
82+
83+
#[ErrorMessage('event.processing_dependency_failed')]
84+
case EVENT_PROCESSING_DEPENDENCY_FAILED = 6403;
85+
86+
// Event configuration errors
87+
#[ErrorMessage('event.configuration_invalid')]
88+
case EVENT_CONFIGURATION_INVALID = 6500;
89+
90+
#[ErrorMessage('event.handler_not_registered')]
91+
case EVENT_HANDLER_NOT_REGISTERED = 6501;
92+
93+
#[ErrorMessage('event.listener_registration_failed')]
94+
case EVENT_LISTENER_REGISTRATION_FAILED = 6502;
95+
96+
// Event system errors
97+
#[ErrorMessage('event.system_unavailable')]
98+
case EVENT_SYSTEM_UNAVAILABLE = 6600;
99+
100+
#[ErrorMessage('event.system_overloaded')]
101+
case EVENT_SYSTEM_OVERLOADED = 6601;
102+
103+
#[ErrorMessage('event.system_maintenance')]
104+
case EVENT_SYSTEM_MAINTENANCE = 6602;
105+
106+
#[ErrorMessage('event.points.insufficient')]
107+
case EVENT_POINTS_INSUFFICIENT = 6603;
108+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
8+
namespace App\Infrastructure\Core\Exception;
9+
10+
use RuntimeException;
11+
use Throwable;
12+
13+
/**
14+
* Event exception class for handling event-related errors
15+
* Used specifically for event delivery and consumption failures.
16+
*/
17+
class EventException extends RuntimeException
18+
{
19+
private ?string $eventType = null;
20+
21+
private ?array $data = null;
22+
23+
public function __construct(string $message = '', int $code = 0, ?Throwable $previous = null)
24+
{
25+
parent::__construct($message, $code, $previous);
26+
}
27+
28+
public function setMessage(string $message): void
29+
{
30+
$this->message = $message;
31+
}
32+
33+
/**
34+
* Set event type for better error tracking.
35+
*/
36+
public function setEventType(string $eventType): self
37+
{
38+
$this->eventType = $eventType;
39+
return $this;
40+
}
41+
42+
/**
43+
* Set event data for debugging purposes.
44+
*/
45+
public function setData(array $data): self
46+
{
47+
$this->data = $data;
48+
return $this;
49+
}
50+
51+
public function getEventType(): ?string
52+
{
53+
return $this->eventType;
54+
}
55+
56+
public function getData(): ?array
57+
{
58+
return $this->data;
59+
}
60+
61+
/**
62+
* Get formatted error context for logging.
63+
*/
64+
public function getErrorContext(): array
65+
{
66+
return [
67+
'event_type' => $this->eventType,
68+
'code' => $this->getCode(),
69+
'message' => $this->getMessage(),
70+
'data' => $this->data,
71+
];
72+
}
73+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
8+
namespace App\Infrastructure\Core\Exception;
9+
10+
use App\ErrorCode\EventErrorCode;
11+
use Throwable;
12+
13+
/**
14+
* Event exception builder for creating event-specific exceptions
15+
* Provides convenient methods for building EventException with context.
16+
*/
17+
class EventExceptionBuilder
18+
{
19+
/**
20+
* Throw an event exception with simplified context.
21+
*
22+
* @param EventErrorCode $errorCode The event error code
23+
* @param string $message Custom error message (optional)
24+
* @param null|string $eventType Event type for context
25+
* @param null|array $data Event data for context
26+
* @param null|Throwable $previous Previous exception
27+
* @return never
28+
*/
29+
public static function throw(
30+
EventErrorCode $errorCode,
31+
string $message = '',
32+
?string $eventType = null,
33+
?array $data = null,
34+
?Throwable $previous = null
35+
): void {
36+
// Create EventException directly with the error code value
37+
$eventException = new EventException(
38+
$message ?: 'Event exception occurred',
39+
$errorCode->value,
40+
$previous
41+
);
42+
43+
// Set event-specific context
44+
if ($eventType !== null) {
45+
$eventException->setEventType($eventType);
46+
}
47+
48+
if ($data !== null) {
49+
$eventException->setData($data);
50+
}
51+
52+
throw $eventException;
53+
}
54+
55+
/**
56+
* Create event exception for consumer execution failure.
57+
*/
58+
public static function consumerExecutionFailed(
59+
string $message = '',
60+
?string $eventType = null,
61+
?array $data = null,
62+
?Throwable $previous = null
63+
): void {
64+
self::throw(
65+
EventErrorCode::EVENT_CONSUMER_EXECUTION_FAILED,
66+
$message,
67+
$eventType,
68+
$data,
69+
$previous
70+
);
71+
}
72+
73+
/**
74+
* Create event exception for delivery failure.
75+
*/
76+
public static function deliveryFailed(
77+
string $message = '',
78+
?string $eventType = null,
79+
?array $data = null,
80+
?Throwable $previous = null
81+
): void {
82+
self::throw(
83+
EventErrorCode::EVENT_DELIVERY_FAILED,
84+
$message,
85+
$eventType,
86+
$data,
87+
$previous
88+
);
89+
}
90+
91+
/**
92+
* Create event exception for data validation failure.
93+
*/
94+
public static function dataValidationFailed(
95+
string $message = '',
96+
?string $eventType = null,
97+
?array $data = null,
98+
?Throwable $previous = null
99+
): void {
100+
self::throw(
101+
EventErrorCode::EVENT_DATA_VALIDATION_FAILED,
102+
$message,
103+
$eventType,
104+
$data,
105+
$previous
106+
);
107+
}
108+
109+
/**
110+
* Create event exception for retry exceeded.
111+
*/
112+
public static function retryExceeded(
113+
string $message = '',
114+
?string $eventType = null,
115+
?array $data = null,
116+
?Throwable $previous = null
117+
): void {
118+
self::throw(
119+
EventErrorCode::EVENT_CONSUMER_RETRY_EXCEEDED,
120+
$message,
121+
$eventType,
122+
$data,
123+
$previous
124+
);
125+
}
126+
127+
/**
128+
* Create event exception for timeout.
129+
*/
130+
public static function timeout(
131+
string $message = '',
132+
?string $eventType = null,
133+
?array $data = null,
134+
?Throwable $previous = null
135+
): void {
136+
self::throw(
137+
EventErrorCode::EVENT_CONSUMER_TIMEOUT,
138+
$message,
139+
$eventType,
140+
$data,
141+
$previous
142+
);
143+
}
144+
}

0 commit comments

Comments
 (0)