44
55namespace Sentry \Attributes ;
66
7+ use Sentry \Serializer \SerializableInterface ;
8+ use Sentry \Util \JSON ;
9+
710/**
811 * @phpstan-type AttributeType 'string'|'boolean'|'integer'|'double'
912 * @phpstan-type AttributeValue string|bool|int|float
@@ -68,7 +71,7 @@ public static function fromValue($value): self
6871 public static function tryFromValue ($ value ): ?self
6972 {
7073 if ($ value === null ) {
71- return null ;
74+ return new self ( ' null ' , ' string ' ) ;
7275 }
7376
7477 if (\is_bool ($ value )) {
@@ -83,14 +86,22 @@ public static function tryFromValue($value): ?self
8386 return new self ($ value , 'double ' );
8487 }
8588
86- if (\is_string ( $ value) || ( \is_object ( $ value ) && method_exists ( $ value , ' __toString ' )) ) {
87- $ stringValue = ( string ) $ value ;
88-
89- if ( empty ( $ stringValue ) ) {
90- return null ;
89+ if ($ value instanceof SerializableInterface ) {
90+ try {
91+ return new self ( JSON :: encode ( $ value -> toSentry ()), ' string ' );
92+ } catch ( \ Throwable $ e ) {
93+ // Ignore the exception and continue trying other methods
9194 }
95+ }
96+
97+ if (\is_string ($ value ) || (\is_object ($ value ) && method_exists ($ value , '__toString ' ))) {
98+ return new self ((string ) $ value , 'string ' );
99+ }
92100
93- return new self ($ stringValue , 'string ' );
101+ try {
102+ return new self (JSON ::encode ($ value ), 'string ' );
103+ } catch (\Throwable $ e ) {
104+ // Ignore the exception
94105 }
95106
96107 return null ;
0 commit comments