Skip to content

Commit

Permalink
fix(php): Catch HTTP request exceptions and rethrow it as a FooApiExc…
Browse files Browse the repository at this point in the history
…eption. (#5923)

Catch HTTP request exceptions and rethrow it as a FooApiException.
  • Loading branch information
Swimburger authored Feb 7, 2025
1 parent 794b359 commit cf2db62
Show file tree
Hide file tree
Showing 117 changed files with 2,359 additions and 8 deletions.
3 changes: 3 additions & 0 deletions fern/pages/changelogs/php-sdk/2025-02-07.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.5.0
**`(feat):`** Add the `__toString()` magic method to all generated class types.

2 changes: 1 addition & 1 deletion generators/browser-compatible-base/src/ast/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Arguments = NamedArgument[] | UnnamedArgument[];

export interface NamedArgument {
name: string;
assignment: AbstractAstNode;
assignment: AbstractAstNode | string;
}

export type UnnamedArgument = AbstractAstNode;
Expand Down
2 changes: 1 addition & 1 deletion generators/csharp/codegen/src/ast/ClassInstantiation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ClassInstantiation extends AstNode {
this.arguments_.forEach((argument, idx) => {
if (isNamedArgument(argument)) {
writer.write(`${argument.name} = `);
argument.assignment.write(writer);
writer.writeNodeOrString(argument.assignment);
} else {
argument.write(writer);
}
Expand Down
2 changes: 1 addition & 1 deletion generators/go-v2/ast/src/ast/utils/writeArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function writeArguments({ writer, arguments_ }: { writer: Writer; argumen

function writeArgument({ writer, argument }: { writer: Writer; argument: Argument }): void {
if (isNamedArgument(argument)) {
argument.assignment.write(writer);
writer.writeNodeOrString(argument.assignment);
} else {
argument.write(writer);
}
Expand Down
2 changes: 1 addition & 1 deletion generators/php/codegen/src/ast/utils/writeArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function writeCompact({ writer, arguments_ }: { writer: Writer; arguments_: Argu
function writeArgument({ writer, argument }: { writer: Writer; argument: Argument }): void {
if (isNamedArgument(argument)) {
writer.write(`${argument.name}: `);
argument.assignment.write(writer);
writer.writeNodeOrString(argument.assignment);
} else {
argument.write(writer);
}
Expand Down
11 changes: 11 additions & 0 deletions generators/php/codegen/src/php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ export function invokeMethod(args: MethodInvocation.Args): MethodInvocation {
return new MethodInvocation(args);
}

export function throwException(args: ClassInstantiation.Args): AstNode {
return codeblock((writer) => {
writer.write("throw ");
writer.writeNode(instantiateClass(args));
});
}

export function map(args: Map.Args): Map {
return new Map(args);
}
Expand Down Expand Up @@ -92,6 +99,10 @@ export function variable(name: string): AstNode {
return codeblock(convertToPhpVariableName(name));
}

export function string(stringValue: string): AstNode {
return codeblock(`"${stringValue}"`);
}

export function mergeArrays(...args: MergeArrays.Args): MergeArrays {
return new MergeArrays(args);
}
Expand Down
43 changes: 43 additions & 0 deletions generators/php/sdk/src/endpoint/http/HttpEndpointGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,49 @@ export class HttpEndpointGenerator extends AbstractEndpointGenerator {
}
writer.dedent();
writer.write("} catch (");
writer.writeNode(this.context.guzzleClient.getRequestExceptionClassReference());
writer.writeLine(" $e) {");
writer.indent();
writer.writeNodeStatement(php.assignVariable(php.variable("response"), "$e->getResponse()"));
writer.controlFlow("if", php.codeblock("$response === null"));
writer.writeNodeStatement(
php.throwException({
classReference: this.context.getBaseExceptionClassReference(),
arguments_: [
{
name: "message",
assignment: "$e->getMessage()"
},
{
name: "previous",
assignment: php.variable("e")
}
]
})
);
writer.endControlFlow();
writer.writeNodeStatement(
php.throwException({
classReference: this.context.getBaseApiExceptionClassReference(),
arguments_: [
{
name: "message",
assignment: php.string("API request failed")
},
{
name: "statusCode",
assignment: "$response->getStatusCode()"
},
{
name: "body",
assignment: "$response->getBody()->getContents()"
}
],
multiline: true
})
);
writer.dedent();
writer.write("} catch (");
writer.writeNode(this.context.getClientExceptionInterfaceClassReference());
writer.writeLine(" $e) {");
writer.indent();
Expand Down
15 changes: 12 additions & 3 deletions generators/php/sdk/src/external/GuzzleClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { SdkGeneratorContext } from "../SdkGeneratorContext";
* The Guzzle HTTP client.
*/
export class GuzzleClient {
public static NAMESPACE = "GuzzleHttp";
public static CLIENT_CLASS_NAME = "Client";
public static CLIENT_INTERFACE_CLASS_NAME = "ClientInterface";
public static readonly NAMESPACE = "GuzzleHttp";
public static readonly EXCEPTION_NAMESPACE = "GuzzleHttp\\Exception";
public static readonly CLIENT_CLASS_NAME = "Client";
public static readonly CLIENT_INTERFACE_CLASS_NAME = "ClientInterface";
public static readonly REQUEST_EXCEPTION_CLASS_NAME = "RequestException";

private context: SdkGeneratorContext;

Expand All @@ -30,6 +32,13 @@ export class GuzzleClient {
});
}

public getRequestExceptionClassReference(): php.ClassReference {
return php.classReference({
name: GuzzleClient.REQUEST_EXCEPTION_CLASS_NAME,
namespace: GuzzleClient.EXCEPTION_NAMESPACE
});
}

public instantiate(): php.ClassInstantiation {
return php.instantiateClass({
classReference: this.getClientClassReference(),
Expand Down
10 changes: 9 additions & 1 deletion generators/php/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
- version: 0.5.1
changelogEntry:
- type: fix
summary: >-
Catch HTTP request exceptions and rethrow it as a FooApiException.
irVersion: 55

- version: 0.5.0
changelogEntry:
- type: feat
summary: >-
Add the `__toString()` magic method to all generated class types.
irVersion: 55

- version: 0.4.0
changelogEntry:
Expand All @@ -28,8 +36,8 @@
$httpClient = new Client(['handler' => $handlerStack]);
$client = new FooClient(['client' => $client]);
```
irVersion: 55

- version: 0.3.2
changelogEntry:
- type: internal
Expand Down
11 changes: 11 additions & 0 deletions seed/php-sdk/accept-header/src/Service/ServiceClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/alias-extends/src/SeedClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/alias/src/SeedClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/any-auth/src/Auth/AuthClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/any-auth/src/User/UserClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/api-wide-base-path/src/Service/ServiceClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/audiences/src/FolderA/Service/ServiceClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/audiences/src/FolderD/Service/ServiceClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions seed/php-sdk/audiences/src/Foo/FooClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cf2db62

Please sign in to comment.