Skip to content

Commit ff077c0

Browse files
cursoragentFedeZara
authored andcommitted
fix(internal): migrate sdk generators to GeneratorError
1 parent 48631df commit ff077c0

61 files changed

Lines changed: 235 additions & 216 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

generators/csharp/sdk/src/SdkGeneratorCli.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { File, GeneratorNotificationService } from "@fern-api/base-generator";
1+
import { File, GeneratorNotificationService, GeneratorError } from "@fern-api/base-generator";
22
import { extractErrorMessage } from "@fern-api/core-utils";
33
import { AbstractCsharpGeneratorCli, CsharpConfigSchema, TestFileGenerator } from "@fern-api/csharp-base";
44
import {
@@ -70,13 +70,13 @@ export class SdkGeneratorCLI extends AbstractCsharpGeneratorCli {
7070
baseApiExceptionClassName &&
7171
baseExceptionClassName === baseApiExceptionClassName
7272
) {
73-
throw new Error("The 'base-api-exception-class-name' and 'base-exception-class-name' cannot be the same.");
73+
throw GeneratorError.internalError("The 'base-api-exception-class-name' and 'base-exception-class-name' cannot be the same.");
7474
}
7575
return customConfig;
7676
}
7777

7878
protected async publishPackage(context: SdkGeneratorContext): Promise<void> {
79-
throw new Error("Method not implemented.");
79+
throw GeneratorError.internalError("Method not implemented.");
8080
}
8181

8282
protected async writeForGithub(context: SdkGeneratorContext): Promise<void> {
@@ -292,13 +292,13 @@ export class SdkGeneratorCLI extends AbstractCsharpGeneratorCli {
292292
endpointSnippets: snippets.endpoints
293293
});
294294
} catch (e) {
295-
throw new Error(`Failed to generate README.md: ${extractErrorMessage(e)}`);
295+
throw GeneratorError.internalError(`Failed to generate README.md: ${extractErrorMessage(e)}`);
296296
}
297297

298298
try {
299299
await this.generateReference({ context });
300300
} catch (e) {
301-
throw new Error(`Failed to generate reference.md: ${extractErrorMessage(e)}`);
301+
throw GeneratorError.internalError(`Failed to generate reference.md: ${extractErrorMessage(e)}`);
302302
}
303303
}
304304
context.logger.debug(`[TIMING] code generation took ${Date.now() - generateStartTime}ms`);

generators/csharp/sdk/src/SdkGeneratorContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fail } from "node:assert";
2-
import { AbstractFormatter, CaseConverter, GeneratorNotificationService, NopFormatter } from "@fern-api/base-generator";
2+
import { AbstractFormatter, CaseConverter, GeneratorNotificationService, NopFormatter, GeneratorError } from "@fern-api/base-generator";
33
import { AsIsFiles, GeneratorContext } from "@fern-api/csharp-base";
44
import { ast, CsharpConfigSchema, Generation } from "@fern-api/csharp-codegen";
55

@@ -355,7 +355,7 @@ export class SdkGeneratorContext extends GeneratorContext {
355355
public resolveEndpoint(service: FernIr.HttpService, endpointId: FernIr.EndpointId): FernIr.HttpEndpoint {
356356
const httpEndpoint = service.endpoints.find((endpoint) => endpoint.id === endpointId);
357357
if (httpEndpoint == null) {
358-
throw new Error(`Failed to find token endpoint ${endpointId}`);
358+
throw GeneratorError.internalError(`Failed to find token endpoint ${endpointId}`);
359359
}
360360
return httpEndpoint;
361361
}

generators/csharp/sdk/src/endpoint/AbstractEndpointGenerator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getOriginalName } from "@fern-api/base-generator";
1+
import { getOriginalName, GeneratorError } from "@fern-api/base-generator";
22
import { assertNever } from "@fern-api/core-utils";
33
import { ast, is, WithGeneration } from "@fern-api/csharp-codegen";
44
import { ExampleGenerator } from "@fern-api/fern-csharp-model";
@@ -131,7 +131,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
131131
return this.Types.CustomPagerClass(itemType);
132132
case "uri":
133133
case "path":
134-
throw new Error(
134+
throw GeneratorError.internalError(
135135
`'${endpoint.pagination.type}' pagination is not supported in C# and should have been skipped.`
136136
);
137137
default:
@@ -178,7 +178,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
178178
return endpoint.pagination.results.property.valueType;
179179
case "uri":
180180
case "path":
181-
throw new Error(
181+
throw GeneratorError.internalError(
182182
`'${endpoint.pagination.type}' pagination is not supported in C# and should have been skipped.`
183183
);
184184
default:
@@ -191,7 +191,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
191191
if (is.Collection.list(listItemType)) {
192192
return listItemType.getCollectionItemType();
193193
}
194-
throw new Error(
194+
throw GeneratorError.internalError(
195195
`Pagination result type for endpoint ${getOriginalName(endpoint.name)} must be a list, but is ${listItemType.fullyQualifiedName}.`
196196
);
197197
}
@@ -242,7 +242,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
242242
if (this.hasPagination(endpoint)) {
243243
return;
244244
}
245-
throw new Error(`Endpoint ${getOriginalName(endpoint.name)} is not a paginated endpoint`);
245+
throw GeneratorError.internalError(`Endpoint ${getOriginalName(endpoint.name)} is not a paginated endpoint`);
246246
}
247247

248248
protected generateEndpointSnippet({
@@ -263,7 +263,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
263263
}): ast.MethodInvocation | undefined {
264264
const service = this.context.ir.services[serviceId];
265265
if (service == null) {
266-
throw new Error(`Unexpected no service with id ${serviceId}`);
266+
throw GeneratorError.internalError(`Unexpected no service with id ${serviceId}`);
267267
}
268268
const serviceFilePath = service.name.fernFilepath;
269269

@@ -388,7 +388,7 @@ export abstract class AbstractEndpointGenerator extends WithGeneration {
388388
parseDatetimes: boolean
389389
): ast.CodeBlock {
390390
if (exampleRequestBody.type === "inlinedRequestBody") {
391-
throw new Error("Unexpected inlinedRequestBody"); // should be a wrapped request and already handled
391+
throw GeneratorError.internalError("Unexpected inlinedRequestBody"); // should be a wrapped request and already handled
392392
}
393393
return this.exampleGenerator.getSnippetForTypeReference({
394394
exampleTypeReference: exampleRequestBody,

generators/csharp/sdk/src/endpoint/http/HttpEndpointGenerator.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getOriginalName } from "@fern-api/base-generator";
1+
import { getOriginalName, GeneratorError } from "@fern-api/base-generator";
22
import { assertNever } from "@fern-api/core-utils";
33
import { ast, is, Writer } from "@fern-api/csharp-codegen";
44
import { FernIr } from "@fern-fern/ir-sdk";
@@ -668,7 +668,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
668668
private writeErrorCase(error: ResponseError, writer: Writer) {
669669
const fullError = this.context.ir.errors[error.error.errorId];
670670
if (fullError == null) {
671-
throw new Error(`Unexpected no error found for error id: ${error.error.errorId}`);
671+
throw GeneratorError.internalError(`Unexpected no error found for error id: ${error.error.errorId}`);
672672
}
673673
writer.writeLine(`case ${fullError.statusCode}:`);
674674
writer.indent();
@@ -1125,7 +1125,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
11251125
// because the pager lambdas work with the actual response data, not the task wrapper
11261126
const unpagedEndpointResponseType = this.getBaseResponseType(endpoint);
11271127
if (!unpagedEndpointResponseType) {
1128-
throw new Error("Internal error; a response type is required for pagination endpoints");
1128+
throw GeneratorError.internalError("Internal error; a response type is required for pagination endpoints");
11291129
}
11301130

11311131
switch (endpoint.pagination.type) {
@@ -1168,7 +1168,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
11681168
break;
11691169
case "uri":
11701170
case "path":
1171-
throw new Error(
1171+
throw GeneratorError.internalError(
11721172
`'${endpoint.pagination.type}' pagination is not supported in C# and should have been skipped.`
11731173
);
11741174
default:
@@ -1211,7 +1211,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
12111211
serviceId: ServiceId;
12121212
}) {
12131213
if (!requestParameter) {
1214-
throw new Error("Request parameter is required for pagination");
1214+
throw GeneratorError.validationError("Request parameter is required for pagination");
12151215
}
12161216

12171217
if (requestParameter.type.isOptional) {
@@ -1360,7 +1360,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
13601360
serviceId: ServiceId;
13611361
}) {
13621362
if (!requestParameter) {
1363-
throw new Error("Request parameter is required for pagination");
1363+
throw GeneratorError.validationError("Request parameter is required for pagination");
13641364
}
13651365

13661366
writer.writeLine("if (request is not null)");
@@ -1471,7 +1471,7 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
14711471
// because the pager lambdas work with the actual response data, not the task wrapper
14721472
const unpagedEndpointResponseType = this.getBaseResponseType(endpoint);
14731473
if (!unpagedEndpointResponseType) {
1474-
throw new Error("Internal error; a response type is required for pagination endpoints");
1474+
throw GeneratorError.internalError("Internal error; a response type is required for pagination endpoints");
14751475
}
14761476

14771477
const queryParameterCodeBlock = endpointSignatureInfo.request?.getQueryParameterCodeBlock();

generators/csharp/sdk/src/endpoint/http/RawClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fail } from "node:assert";
2-
import { Arguments, getWireValue } from "@fern-api/base-generator";
2+
import { Arguments, getWireValue, GeneratorError } from "@fern-api/base-generator";
33
import { assertNever } from "@fern-api/core-utils";
44
import { ast, WithGeneration, Writer } from "@fern-api/csharp-codegen";
55
import { FernIr } from "@fern-fern/ir-sdk";
@@ -157,7 +157,7 @@ export class RawClient extends WithGeneration {
157157
};
158158
case "multipartform": {
159159
if (endpoint.requestBody?.type !== "fileUpload") {
160-
throw new Error("Internal error; Multipart form requests are only supported for file uploads");
160+
throw GeneratorError.internalError("Internal error; Multipart form requests are only supported for file uploads");
161161
}
162162
const requestBody = endpoint.requestBody;
163163
const varName = "multipartFormRequest_";
@@ -379,7 +379,7 @@ export class RawClient extends WithGeneration {
379379
writer.write(`{${counter++}}`);
380380
const reference = pathParameterReferences[part.pathParameter];
381381
if (reference == null) {
382-
throw new Error(
382+
throw GeneratorError.internalError(
383383
`Failed to find request parameter for the endpoint ${endpoint.id} with path parameter ${part.pathParameter}`
384384
);
385385
}

generators/csharp/sdk/src/endpoint/request/EndpointRequestFactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GeneratorError } from "@fern-api/base-generator";
12
import { FernIr } from "@fern-fern/ir-sdk";
23

34
type HttpEndpoint = FernIr.HttpEndpoint;
@@ -43,7 +44,7 @@ export function createEndpointRequest({
4344
}
4445
},
4546
_other: () => {
46-
throw new Error("");
47+
throw GeneratorError.internalError("");
4748
}
4849
});
4950
}

generators/csharp/sdk/src/endpoint/snippets/EndpointSnippetsGenerator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getOriginalName } from "@fern-api/base-generator";
1+
import { getOriginalName, GeneratorError } from "@fern-api/base-generator";
22
import { WithGeneration } from "@fern-api/csharp-codegen";
33
import { FernIr } from "@fern-fern/ir-sdk";
44

@@ -120,14 +120,14 @@ export class EndpointSnippetsGenerator extends WithGeneration {
120120
snippet.autogenerated.forEach((s) => {
121121
const snippet = formattedSnippets[index++];
122122
if (snippet === undefined) {
123-
throw new Error("Internal error; No snippet found for autogenerated example");
123+
throw GeneratorError.internalError("Internal error; No snippet found for autogenerated example");
124124
}
125125
s.endpointCall = snippet;
126126
});
127127
snippet.userSpecified.forEach((s) => {
128128
const snippet = formattedSnippets[index++];
129129
if (snippet === undefined) {
130-
throw new Error("Internal error; No snippet found for user specified example");
130+
throw GeneratorError.internalError("Internal error; No snippet found for user specified example");
131131
}
132132
s.endpointCall = snippet;
133133
});

generators/csharp/sdk/src/endpoint/utils/getContentTypeFromRequestBody.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GeneratorError } from "@fern-api/base-generator";
12
import { FernIr } from "@fern-fern/ir-sdk";
23

34
type HttpEndpoint = FernIr.HttpEndpoint;
@@ -12,7 +13,7 @@ export function getContentTypeFromRequestBody(endpoint: HttpEndpoint): string |
1213
fileUpload: (_) => undefined,
1314
bytes: (body) => body.contentType,
1415
_other: (body) => {
15-
throw new Error(`Unexpected request body type: ${body.type}`);
16+
throw GeneratorError.internalError(`Unexpected request body type: ${body.type}`);
1617
}
1718
});
1819
}

generators/csharp/sdk/src/environment/MultiUrlEnvironmentGenerator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GeneratorError } from "@fern-api/base-generator";
12
import { CSharpFile, FileGenerator } from "@fern-api/csharp-base";
23
import { ast } from "@fern-api/csharp-codegen";
34
import { join, RelativeFilePath } from "@fern-api/fs-utils";
@@ -51,7 +52,7 @@ export class MultiUrlEnvironmentGenerator extends FileGenerator<CSharpFile> {
5152
arguments_: Object.entries(environment.urls).map(([id, url]) => {
5253
const baseUrl = this.multiUrlEnvironments.baseUrls.find((url) => url.id === id);
5354
if (baseUrl == null) {
54-
throw new Error(`Failed to find base url with id ${id}`);
55+
throw GeneratorError.referenceError(`Failed to find base url with id ${id}`);
5556
}
5657
return {
5758
name: this.case.pascalSafe(baseUrl?.name) ?? "",

generators/csharp/sdk/src/inferred-auth/InferredAuthTokenProviderGenerator.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GeneratorError } from "@fern-api/base-generator";
12
import { CSharpFile, FileGenerator } from "@fern-api/csharp-base";
23
import { ast, is } from "@fern-api/csharp-codegen";
34
import { join, RelativeFilePath } from "@fern-api/fs-utils";
@@ -215,7 +216,7 @@ export class InferredAuthTokenProviderGenerator extends FileGenerator<CSharpFile
215216
this.bufferInMinutesField == null ||
216217
this.expiryProperty == null
217218
) {
218-
throw new Error("Caching fields should be defined when expiryProperty is not null");
219+
throw GeneratorError.internalError("Caching fields should be defined when expiryProperty is not null");
219220
}
220221

221222
const cachedHeadersField = this.cachedHeadersField;
@@ -350,14 +351,14 @@ export class InferredAuthTokenProviderGenerator extends FileGenerator<CSharpFile
350351
if (is.ClassReference(typeRef)) {
351352
return typeRef;
352353
}
353-
throw new Error(
354+
throw GeneratorError.internalError(
354355
`Failed to get request class reference for inferred auth token endpoint. ` +
355356
`Expected ClassReference but got ${typeRef.constructor.name}. ` +
356357
`Service ID: ${this.tokenEndpointReference.serviceId}, ` +
357358
`Endpoint ID: ${this.tokenEndpointReference.endpointId}`
358359
);
359360
}
360-
throw new Error(
361+
throw GeneratorError.internalError(
361362
`Failed to get request type reference for inferred auth token endpoint. ` +
362363
`Service ID: ${this.tokenEndpointReference.serviceId}, ` +
363364
`Endpoint ID: ${this.tokenEndpointReference.endpointId}. ` +

0 commit comments

Comments
 (0)