Skip to content

Commit

Permalink
fix: format process errors to be more user friendly PE-7327
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Dec 18, 2024
1 parent a4e178d commit 3449e32
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/common/contracts/ao-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ export class AOProcess implements AOContract {
result,
});

const error = errorMessageFromOutput(result);
if (error !== undefined) {
throw new Error(error);
}

if (result.Messages === undefined || result.Messages.length === 0) {
this.logger.debug(
`Process ${this.processId} does not support provided action.`,
Expand All @@ -89,15 +94,7 @@ export class AOProcess implements AOContract {
`Process ${this.processId} does not support provided action.`,
);
}

const tagsOutput = result.Messages?.[0]?.Tags;
const messageData = result.Messages?.[0]?.Data;
const errorData = result.Error;
const error =
errorData || tagsOutput?.find((tag) => tag.name === 'Error')?.value;
if (error) {
throw new Error(`${error}${messageData ? `: ${messageData}` : ''}`);
}

// return undefined if no data is returned
if (this.isMessageDataEmpty(messageData)) {
Expand All @@ -109,7 +106,7 @@ export class AOProcess implements AOContract {
} catch (e) {
attempts++;
this.logger.debug(`Read attempt ${attempts} failed`, {
error: e,
error: e instanceof Error ? e.message : e,
tags,
});
lastError = e;
Expand Down Expand Up @@ -176,12 +173,8 @@ export class AOProcess implements AOContract {
processId: this.processId,
});

const errorData = output.Error;
const error =
errorData ||
output.Messages?.[0]?.Tags?.find((tag) => tag.name === 'Error')
?.value;
if (error) {
const error = errorMessageFromOutput(output);
if (error !== undefined) {
throw new WriteInteractionError(error);
}

Expand Down Expand Up @@ -235,3 +228,28 @@ export class AOProcess implements AOContract {
throw lastError;
}
}

function errorMessageFromOutput(output: {
Error?: string;
Messages?: { Tags?: { name: string; value: string }[] }[];
}): string | undefined {
const errorData = output.Error;
if (errorData !== undefined) {
// TODO: Could clean this one up too, current error is verbose, but not always deterministic for parsing
// Throw the whole raw error if AO process level error
return errorData;
}

const error = output.Messages?.[0]?.Tags?.find(
(tag) => tag.name === 'Error',
)?.value;
if (error !== undefined) {
// from [string "aos"]:6846: Name is already registered
const lineNumber = error.match(/\d+/)?.[0];
const message = error.replace(/\[string "aos"\]:\d+:/, '');
// to more user friendly: Name is already registered (line 6846)
return `${message} (line ${lineNumber})`.trim();
}

return undefined;
}

0 comments on commit 3449e32

Please sign in to comment.