1
0
Fork 0
mirror of https://github.com/archtechx/airwire.git synced 2025-12-12 02:34:04 +00:00

improve error handling

This commit is contained in:
Samuel Štancl 2021-05-21 18:45:19 +02:00
parent 9fffe87970
commit 459b74eb18
3 changed files with 16 additions and 10 deletions

View file

@ -187,7 +187,7 @@ export class Component<AirwireComponent = TypeMap[keyof TypeMap]>
return reason return reason
}) })
.then((json: ComponentResponse<AirwireComponent>) => { // todo this then() shouldn't execute if previous catch() executes .then((json: ComponentResponse<AirwireComponent>) => {
if (json?.metadata?.errors) { if (json?.metadata?.errors) {
this.errors = json.metadata.errors this.errors = json.metadata.errors
} }
@ -227,7 +227,11 @@ export class Component<AirwireComponent = TypeMap[keyof TypeMap]>
let result = null; let result = null;
try { try {
result = callback(); result = callback();
} catch (e) { } } catch (e) {
this.pausedRequests = false;
throw e
}
this.pausedRequests = false; this.pausedRequests = false;

View file

@ -32,15 +32,17 @@ class TypehintConverter
}; };
} }
public function convertType(string $php): string public function convertType(string $php, string $target = 'property'): string
{ {
if (class_exists($php)) { if (class_exists($php)) {
if (is_subclass_of($php, Model::class) && ($model = $php::first())) { if (is_subclass_of($php, Model::class) && ($model = $php::first())) {
return $this->convertModel($model); return $target === 'parameter'
? $this->convertModel($model) . '|string|number' // Models can be resolved from IDs
: $this->convertModel($model);
} }
if (is_subclass_of($php, Collection::class) && ($model = $php::first())) { if (is_subclass_of($php, Collection::class)) {
return 'array'; return 'any'; // Later maybe typed arrays?
} }
return 'any'; return 'any';
@ -200,7 +202,7 @@ class TypehintConverter
if (isset($object->$property) && gettype($object->$property) === $type->getName()) { if (isset($object->$property) && gettype($object->$property) === $type->getName()) {
$results[] = $this->typeFromValue($object->$property); $results[] = $this->typeFromValue($object->$property);
} else { } else {
$results[] = $this->convertType($type->getName()); $results[] = $this->convertType($type->getName(), 'property');
} }
} }
@ -226,14 +228,14 @@ class TypehintConverter
$types[] = 'null'; $types[] = 'null';
} }
$parameters[$parameter->getName()] = join(' | ', array_map(fn (ReflectionNamedType $type) => $this->convertType($type->getName()), $types)); $parameters[$parameter->getName()] = join(' | ', array_map(fn (ReflectionNamedType $type) => $this->convertType($type->getName(), 'parameter'), $types));
} }
$parameters = collect($parameters)->map(fn (string $type, string $name) => "{$name}: {$type}")->join(', '); $parameters = collect($parameters)->map(fn (string $type, string $name) => "{$name}: {$type}")->join(', ');
$return = match ($type = $reflection->getReturnType()) { $return = match ($type = $reflection->getReturnType()) {
null => 'any', null => 'any',
default => $this->convertType($type), default => $this->convertType($type, 'return'),
}; };
return "{$method}(" . $parameters . "): AirwirePromise<{$return}>;"; return "{$method}(" . $parameters . "): AirwirePromise<{$return}>;";

View file

@ -163,7 +163,7 @@ class TypehintComponent extends Component
#[Wired] #[Wired]
public Product $model; public Product $model;
#[Wired] #[Encode(method: 'getKey')] // todo add the same feature for Decode #[Wired] #[Encode(method: 'getKey')] // todo add the same feature for Decode (but then we may have to update the type generator)
public Product $model2; public Product $model2;
#[Wired] #[Wired]