MetadataKeyNotFoundException
The MetadataKeyNotFoundException exception is thrown when attempting to store metadata with keys that are not in the model's allowed metadata list.
Namespace
JobMetric\Metadata\Exceptions\MetadataKeyNotFoundException
Overview
This exception is thrown when:
- Storing metadata via model attributes with keys not in the
$metadataarray - The model has a restricted
$metadatalist (not['*']) - One or more keys in the metadata array are not allowed
Exception Details
Constructor
public function __construct(string|array $key, int $code = 400, ?Throwable $previous = null)
Parameters:
string|array $key- The key(s) that were not found/allowedint $code- Exception code (default:400)Throwable|null $previous- Previous exception for chaining
When It's Thrown
Storing via Model Attributes
class Product extends Model
{
use HasMeta;
protected array $metadata = ['color', 'size'];
}
// This will throw MetadataKeyNotFoundException
$product = Product::create([
'name' => 'Test',
'metadata' => [
'color' => 'red', // OK
'weight' => 500, // Not allowed!
],
]);
Handling the Exception
Check Keys Before Storing
try {
$product = Product::create([
'name' => 'Test',
'metadata' => $metadata,
]);
} catch (MetadataKeyNotFoundException $e) {
// Handle: Invalid metadata keys
return response()->json([
'error' => 'Invalid metadata keys',
'keys' => $e->getMessage(),
], 400);
}
Validate Before Assignment
$allowedKeys = $product->getMetaKeys();
$metadata = request()->input('metadata', []);
$invalidKeys = array_diff(array_keys($metadata), $allowedKeys);
if (!empty($invalidKeys) && !in_array('*', $allowedKeys)) {
throw new MetadataKeyNotFoundException($invalidKeys);
}