You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The latest implementation of VS Code's component model converts result types returned from function and methods into a normal return type (the Ok part) and an exception.
use error-code inside the calculator world and change the signature of the calc function to return result<u32, error-code>. The world will look like this:
world calculator {
use types.{ operation, error-code };
import log: func(msg: string);
export calc: func(o: operation) -> result<u32, error-code>;
}
run npm run generate:model
observe: a new calculator.ts file got created with as an ErrorCode enumeration and a corresponding exception.
adapt the Rust code in lib.rs to return Result::Err(ErrorCode::DivideByZero) in case a division by Zero happens.
adapt the TypeScript code in extension.ts to catch error when calling a Div operation. Ensure that the return exception if of type Types.ErrorCode.Error_ and its value is error.value === Types.ErrorCode.divideByZero. Something like
try {
const div = Types.Operation.Div({ left: 10, right: 0 });
channel.appendLine(`Div ${api.calc(div)}`);
} catch (error) {
if (error instanceof Types.ErrorCode.Error_ && error.value === Types.ErrorCode.divideByZero) {
channel.appendLine('Division by zero not allowed');
}
}
The text was updated successfully, but these errors were encountered:
Refs: microsoft/vscode-wasm#177
Complexity: 3
Create Issue
The latest implementation of VS Code's component model converts
result
types returned from function and methods into a normal return type (the Ok part) and an exception.Prerequisites
Testplan
You can make your own component model sample using the two blog posts as a starter: https://code.visualstudio.com/blogs/2024/05/08/wasm and https://code.visualstudio.com/blogs/2024/06/07/wasm-part2
Easier is to clone the
vscode-extension-samples
and start off thewasm-component-model
samplerun
npm install
add an
error-code
enumeration to thecalculator.wit
file inside thetypes
interface. Something like this:error-code
inside the calculator world and change the signature of the calc function to returnresult<u32, error-code>
. The world will look like this:npm run generate:model
calculator.ts
file got created with as an ErrorCode enumeration and a corresponding exception.lib.rs
to returnResult::Err(ErrorCode::DivideByZero)
in case a division by Zero happens.extension.ts
to catch error when calling aDiv
operation. Ensure that the return exception if of typeTypes.ErrorCode.Error_
and its value iserror.value === Types.ErrorCode.divideByZero
. Something likeThe text was updated successfully, but these errors were encountered: