Skip to content

Commit

Permalink
Merge pull request #166 from Cratis:fix/command-errors
Browse files Browse the repository at this point in the history
If command fails during fetch - we now return a failed command
  • Loading branch information
einari authored Sep 1, 2024
2 parents 4e0b9af + 5d8ec87 commit 74a42f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
22 changes: 13 additions & 9 deletions Source/JavaScript/Applications/commands/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export abstract class Command<TCommandContent = {}, TCommandResponse = {}> imple
private _initialValues: any = {};
private _hasChanges = false;
private _callbacks: Callback[] = [];

/**
* Initializes a new instance of the {@link Command<,>} class.
* @param _responseType Type of response.
Expand Down Expand Up @@ -62,18 +62,22 @@ export abstract class Command<TCommandContent = {}, TCommandResponse = {}> imple
headers[Globals.microserviceHttpHeader] = Globals.microservice;
}

const response = await fetch(actualRoute, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
this.setInitialValuesFromCurrentValues();

try {
const response = await fetch(actualRoute, {
method: 'POST',
headers,
body: JSON.stringify(payload)
});
this.setInitialValuesFromCurrentValues();

if( response.status === 404) {
return CommandResult.failed([`Command not found at route '${actualRoute}'`]) as CommandResult<TCommandResponse>;
}

const result = await response.json();
return new CommandResult(result, this._responseType, this._isResponseTypeEnumerable);
} catch (ex) {
return CommandResult.empty as CommandResult<TCommandResponse>;
return CommandResult.failed([`Error during server call: ${ex}`]) as CommandResult<TCommandResponse>;
}
}

Expand Down
14 changes: 14 additions & 0 deletions Source/JavaScript/Applications/commands/CommandResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ export class CommandResult<TResponse = {}> implements ICommandResult<TResponse>
response: null
}, Object, false);

static failed = (exceptionMessages: string[]): CommandResult => {
return new CommandResult({
correlationId: Guid.empty.toString(),
isSuccess: false,
isAuthorized: true,
isValid: true,
hasExceptions: true,
validationResults: [],
exceptionMessages: exceptionMessages,
exceptionStackTrace: '',
response: null
}, Object, false);
};

/** @inheritdoc */
readonly correlationId: Guid;

Expand Down

0 comments on commit 74a42f5

Please sign in to comment.