Fix GitLab setup command (#445)

* Properly log errors in the console for unknown faults

* Fix wrong this in bind

* bugfix
This commit is contained in:
Will Hunt 2022-08-22 10:02:22 +01:00 committed by GitHub
parent 49bc94e9ad
commit f8b0dc40b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 8 deletions

1
changelog.d/445.bugfix Normal file
View File

@ -0,0 +1 @@
Fixed issue where `!hookshot gitlab project` commands would fail with a "Failed to handle command." error.

View File

@ -88,10 +88,31 @@ export function compileBotCommands(...prototypes: Record<string, BotCommandFunct
}
}
interface CommandResultNotHandled {
handled: false;
}
interface CommandResultSuccess {
handled: true;
result: BotCommandResult;
}
interface CommandResultErrorUnknown {
handled: true;
humanError?: string;
error: Error;
}
interface CommandResultErrorHuman {
handled: true;
humanError: string;
error?: Error;
}
export async function handleCommand(
userId: string, command: string, botCommands: BotCommands, obj: unknown, permissionCheckFn: PermissionCheckFn,
defaultPermissionService?: string, prefix?: string)
: Promise<{handled: false}|{handled: true, result: BotCommandResult}|{handled: true, error: string, humanError?: string}> {
: Promise<CommandResultNotHandled|CommandResultSuccess|CommandResultErrorUnknown|CommandResultErrorHuman> {
if (prefix) {
if (!command.startsWith(prefix)) {
return {handled: false};
@ -106,10 +127,10 @@ export async function handleCommand(
if (command) {
const permissionService = command.permissionService || defaultPermissionService;
if (permissionService && !permissionCheckFn(permissionService, command.permissionLevel || BridgePermissionLevel.commands)) {
return {handled: true, error: "You do not have permission to use this command."};
return {handled: true, humanError: "You do not have permission to use this command."};
}
if (command.requiredArgs && command.requiredArgs.length > parts.length - i) {
return {handled: true, error: "Missing at least one required parameter."};
return {handled: true, humanError: "Missing at least one required parameter."};
}
const args = parts.slice(i);
if (command.includeUserId) {
@ -121,9 +142,9 @@ export async function handleCommand(
} catch (ex) {
const commandError = ex as CommandError;
if (ex instanceof ApiError) {
return {handled: true, error: ex.error, humanError: ex.error};
return {handled: true, humanError: ex.error};
}
return {handled: true, error: commandError.message, humanError: commandError.humanError};
return {handled: true, error: commandError, humanError: commandError.humanError};
}
}
}

View File

@ -845,7 +845,7 @@ export class Bridge {
messageClient: this.messageClient,
storage: this.storage,
github: this.github,
getAllConnectionsOfType: this.connectionManager.getAllConnectionsOfType.bind(this),
getAllConnectionsOfType: this.connectionManager.getAllConnectionsOfType.bind(this.connectionManager),
},
this.getOrCreateAdminRoom.bind(this),
)

View File

@ -46,8 +46,8 @@ export abstract class CommandConnection<StateType extends IConnectionState = ICo
// Not for us.
return false;
}
if ("error" in commandResult) {
const { humanError, error} = commandResult;
if ("error" in commandResult || "humanError" in commandResult) {
const { humanError, error } = commandResult;
await this.botClient.sendEvent(this.roomId, "m.reaction", {
"m.relates_to": {
rel_type: "m.annotation",