cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

Handling of return types != 200 #113

Closed StefanKoenigMUC closed 4 years ago

StefanKoenigMUC commented 4 years ago

Hi there,

still very pleased by ng-openapi-gen (.. regarding the other bug in windows I opened weeks ago, still only agenda, but project work is overwhelming right now..).

But for now there is another problem I run into:

I've specification like this:

    "/api/entities/GetSomething/{type}/{id}": {
      "get": {
        "tags": [
          "Entities"
        ],
        "parameters": [
          {
            "name": "type",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "nullable": true
            }
          },
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GetEntityResult"
                }
              }
            }
          },
          "404": {
            "description": "Not Found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        }
      }
    },

for success case everything is fine, but the non-success case -> 404, I'm not sure how to handle this.

Accessing the service looks like this.

    this.myService.apiEntitiesGetSomethingGet({ type: objectType, id: this.area.parameters.editedObject.Id })
      .pipe(take(1))
      .subscribe((result) => this.setModel(result),
        error => console.log('Could not retrieve edited Error: ' + JSON.stringify(error)),
        () => console.log('OK');

if an error is thrown, I'm redirected to the error =>, but the error itself is undefined.

Is there a way to access the ProblemDetails entity? (or is there any other way available for handling return codes != 200?)

Thanks in advance!, Stefan

luisfpg commented 4 years ago

the Observables are handled by Angular HttpClient. So, this should work:

service.method(params).pipe(first()).subscribe(
    result => console.log(`Success! ${JSON.stringify(result)}`),
    (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      // Client-side error
      console.error('Client-side request error');
      console.error(err.error);
    } else {
      // Server-generated error
      let error = err.error;
      if (typeof error === 'string') {
        try {
          error = JSON.parse(error);
        } catch (e) {
          error = null;
        }
      }
      console.log(`Error ${err.status}: ${JSON.stringify(error)}`);
    });
)
StefanKoenigMUC commented 4 years ago

thanks a lot, that helped me! :-)