fix:this->error can be reset in the middle of validateOrThrow #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Caught a problem in production where some times validateOrThrow will throw a type error
I wasn't able to reproduce the problem in a unit test (even using an exact copy of this DealData and an export-import of the Account). I ended up copy-pasting the implementation of
validateOrThrow
into Tinker on the affected Kubernetes pod, where I was able to reproduce, and experimenting with echo statements and iteratively rewriting.The problem appears to be that in a complicated app, the
Log::debug
call can synchronously involve other systems, some of which may also be using this Facade to validate other data. In some roundabout wayLog::debug
is causing SchemaValidatorService::error to be reset tonull
-- because SchemaValidatorProvider is intentionally designed to be asingleton
that$error
property is effectively a global. Yikes!The fix in Tinker was to get error into a local variable at the top of the method. But I think the right fix for the library is to obsolete the pattern where
validate
returns a boolean, then the race is on to get theerror
-- instead I introduced a method that returns the full\Opis\JsonSchema\ValidationResult
(both boolean valid plus the errors array, in one bundle) and refactored validateOrThrow to use that method. I also totally obsoleted the idea of getting theerror
in a separate call, since as a singleton that can't ever be totally safe.