fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext } from '@hey-api/codegen-core';
2
3import { py } from '../../ts-python';
4import type { MaybePyDsl } from '../base';
5import { PyDsl } from '../base';
6// import { LiteralPyDsl } from '../expr/literal';
7
8const Mixed = PyDsl<py.RaiseStatement>;
9
10export class RaisePyDsl extends Mixed {
11 readonly '~dsl' = 'RaisePyDsl';
12
13 protected _error?: string | MaybePyDsl<py.Expression>;
14 // protected msg?: string | MaybePyDsl<py.Expression>;
15
16 constructor(error?: string | MaybePyDsl<py.Expression>) {
17 super();
18 this._error = error;
19 }
20
21 override analyze(ctx: AnalysisContext): void {
22 super.analyze(ctx);
23 ctx.analyze(this._error);
24 // ctx.analyze(this.msg);
25 }
26
27 // /** Sets the message argument for the exception (e.g. `raise ValueError('msg')`). */
28 // message(value: string | MaybePyDsl<py.Expression>): this {
29 // this.msg = value;
30 // return this;
31 // }
32
33 override toAst() {
34 // Python's `raise` can be bare (re-raise), or `raise <expr>`.
35 // Unlike JS `throw new Error(msg)`, Python uses `raise ErrorType(msg)` directly,
36 // so the caller constructs the call expression themselves.
37 const errorNode = this._error ? this.$node(this._error) : undefined;
38 return py.factory.createRaiseStatement(errorNode);
39 }
40}