Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 18:50
    JavaScriptDude commented #116
  • 18:42
    JavaScriptDude commented #116
  • 18:36
    plowman commented #116
  • May 23 21:07
    temurchichua commented #228
  • May 23 14:41
    sillydan1 commented #440
  • May 23 06:13
    radiantone commented #440
  • May 23 05:15
    sillydan1 commented #440
  • May 18 14:48
    radiantone edited #440
  • May 18 14:48
    radiantone edited #440
  • May 18 14:47
    radiantone opened #440
  • May 18 14:47
    radiantone labeled #440
  • May 15 05:51
    cmpunches labeled #439
  • May 15 05:51
    cmpunches opened #439
  • May 14 08:37
    thedumbterminal edited #438
  • May 14 08:36
    thedumbterminal labeled #438
  • May 14 08:36
    thedumbterminal opened #438
  • May 14 00:07
    boompig commented #437
  • May 14 00:05
    boompig opened #437
  • May 14 00:05
    boompig labeled #437
  • May 13 17:49
    JavaScriptDude commented #116
John Chittum
@j5awry
@api.errorhandler associates an exception of your choice with a message and http message to send back via a func. so
@api.errorhandler(BusinessError)
def  handle_business_error(error):
    return {"message": "A logical error has occurred"}, 418

@api.errorhandler(AnotherBusiness):
def hand_another_business_error(error):
    return {"message": "a different business logic error"}, 451
E-Kalla
@E-Kalla
I can catch No Http exception with @api.errorhandler ?
E-Kalla
@E-Kalla
Here , how can I catch the error from the failed business function without try/except ?
John Chittum
@j5awry

provide api.errorhandler with the error you want need it to catch

https://pastebin.ubuntu.com/p/skqH6k5Nn9/

i don't know what db_scenario.to_domain() will raise, but just setup handlers for each one of those. You can see in my example, I'm just calling raise without an try/except code and it's caught, a 418 is returned, along with the message i passed it
you can also use a hierarchy -- so, let's say you've got a bunch of db errors, and they all derive from a single err
class MyDbError(Exception):
   pass

class BadQuery(MyDbError):
    pass

class BadFormatting(MyDbError):
    pass
you can feed api.errorhandler(MyDbError) and catch everything that has inherited MyDbError
I tried the code on the pastebin but It doesn't work , did I miss something ?
I should have Status:418 on postman and not 500
John Chittum
@j5awry
what's it actually returning? What's the 500 and the stacktrace in the log?
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
John Chittum
@j5awry
As an example of "add decorators to Swagger?" Sure. but how flasgger generates it's view is a very different workflow than restx
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
Was an idea
John Chittum
@j5awry
Looking at it, we might be able to do something similar to what happens here: https://github.com/flasgger/flasgger/blob/master/flasgger/base.py#L582
the way the view is generally built is very different, but creating a couple wrap funcs that can wrap an entire view may be possible
but it'll take some digging. is there an issue up for this already (i still need to dig through more issue backlog)
E-Kalla
@E-Kalla

what's it actually returning? What's the 500 and the stacktrace in the log?

I sent some screen , I reproduced your pastebin to catch an exception https://prnt.sc/123j36f and handle it with my function https://prnt.sc/123j4lg but when I test the request I have HttpError 500 instead of 418

John Chittum
@j5awry
what are the app logs saying in conjunction with the 500? Are the app logs quiet and it's just returning 500, or is the application throwing a stacktrace?
E-Kalla
@E-Kalla

what are the app logs saying in conjunction with the 500? Are the app logs quiet and it's just returning 500, or is the application throwing a stacktrace?

https://prnt.sc/1258amm
Here is the stacktrace

E-Kalla
@E-Kalla
https://prnt.sc/1259av5 I have Cors missing allow origin
John Chittum
@j5awry
Hm, i'm still not sure what's happening here. Sorry. Something inside the app is generating an error that's not getting caught.
i can't tell from that stacktrace if it's related to the error_handling or something else (such as CORS)
E-Kalla
@E-Kalla
np, thank you for yours answers ;)
John Chittum
@j5awry
I'd say if you can start on a more simple example -- something as basic as possible, and then start adding layers, and catching when it works or fails
if it fails at the very beginning, say, just instantiating an API, custom error handler, and raising, then we'll know it's something in the custom error, error handler, etc. Then add in the marshalling, cors, etc
E-Kalla
@E-Kalla

hello @j5awry
I successed to make the simple example on your pastebin working.

I tried to implement it on a very simple get routes but It didnt work , https://pastebin.ubuntu.com/p/8NWdRCgz38/
I made to many tests and it always return http error 500.
I'll continue my research

E-Kalla
@E-Kalla
Ok , I found It https://github.com/pallets/flask/blob/2ab91446f608755affba035650c1dd74b825a018/flask/app.py#L1446-L1450
It looks flask raise http Error 500 before the exception get caught by the handler with debug mode on
I disabled debug mod and It works
E-Kalla
@E-Kalla
Hey ,
I needed the debug mode on so I found another solution
I set PROPAGATE_EXCEPTIONS = False
https://prnt.sc/12956at
But It looks weird because we can see here https://github.com/python-restx/flask-restx/blob/66471c5223e0237762fc2722d680229bb68e0d16/flask_restx/api.py#L675
It says the opposite, so I am a little confused
@j5awry
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
@j5awry After many failed attempts for basic auth, i think best is disable docs in production
John Chittum
@j5awry
@Abdur-rahmaanJ If you don't mind me asking, is this an on-prem or cloud hosted? And is it possible to add authentication at other layers? It depends a lot on your setup, but something like using JWT with NGinx, or x509 based auth at Apache Webserver are other possibilities. But there's a lot of nuance there. I do think we need to figure out a better way of handling the pre-generated doc endpoint, and I think what flasgger is doing could be an interesting entry point. but it's going to take code change in flask-restx
@E-Kalla : what version of flask-restx are you using again? And if you haven't, could you open an issue on our github? I think we're seeing various things that'd be nice to track in an issue.
E-Kalla
@E-Kalla

@E-Kalla : what version of flask-restx are you using again? And if you haven't, could you open an issue on our github? I think we're seeing various things that'd be nice to track in an issue.

I'm using flask-restx 0.3.0

E-Kalla
@E-Kalla
I opened an issue here python-restx/flask-restx#305 tell me if something is not clear , first time for me
Jason Weir
@Gidgidonihah
I just tracked down a bug in our own code related to flask_restx error handling when swapping from flask_restplus. The bug was that we were relying on the restplus bug that swallowed errors and we didn't know it. We had an error handler to handle custom error codes which should have dropped us into this block but instead we hit the else and threw a ValueError on line 705. Because flask_restplus swallowed errors, it worked, when swapping, it no longer did.
We tracked it down to the fact that the error handler was registered on the flask api, rather than the api instance. Fixed that and it worked as expected, but my questions is:
Should it have ignored error handlers on the app and only looked at api error handlers?
If the answer is yes, so be it, but I had to ask
John Chittum
@j5awry

@Gidgidonihah : Reading the code, the intent is that errorhandlers get checked for the restx.API and it's children (restx.API.namespace). It wouldn't make sense, to me, for the framework to go up the chain as well.

So self + children vs. self + parents + children. self + children makes sense to me for checking handlers. But I may be misinterpreting the question?

@E-Kalla : thank you. I think there's good information to start looking at things more closely. Sorry we can't react quickly. The community is small. Having it in an issue will help us keep track of comments and have higher visibility. I can also cross-check against current and past issues a bit easier.
Jason Weir
@Gidgidonihah
@j5awry I don't think you are misinterpreting. That was how I read the code as well. However, it did lead to an easy bug like this. Let me phrase the question like this. When would I want to register a handler on the flask app, and not have the framework use it? In this case, self is a child of the app, so it could also make sense that it bubbles up to that level, as it were.
1 reply
E-Kalla
@E-Kalla
Hello,
I have a question about marshal models.
How can I make a self reference in a model ?
https://prnt.sc/12kvzl2
I want my model 'error' can contain himself recursively.
But i'm struggling with ...
E-Kalla
@E-Kalla
actually , I want to this : https://marshmallow.readthedocs.io/en/stable/nesting.html#nesting-a-schema-within-itself
Is it possible without using marshmallow ?
Abdur-Rahmaan Janhangeer
@Abdur-rahmaanJ
@j5awry See https://aaronluna.dev/series/flask-api-tutorial/part-3/ "LINES 6, 14: The API will implement Bearer token authentication ..." and the comment below it

@Abdur-rahmaanJ If you don't mind me asking, is this an on-prem or cloud hosted? And is it possible to add authentication at other layers? It depends a lot on your setup, but something like using JWT with NGinx, or x509 based auth at Apache Webserver are other possibilities. But there's a lot of nuance there. I do think we need to figure out a better way of handling the pre-generated doc endpoint, and I think what flasgger is doing could be an interesting entry point. but it's going to take code change in flask-restx

Cloud-hosted, server-based auth is a great option also but see the above

1 reply
John Chittum
@j5awry
@E-Kalla : woof, self-reference. So, JSONSchema at a certain point started enabling more of that. I'd have to check on versioning. a lot of how the models are written and validated is based on JSONSchema v. 4. I'd have to look more closely at how to deal with something nested. But, unfortunately, restx models aren't as feature rich as Marshmallow
Henshal B
@henshalb
Hello, I have got a query.. How to add an additional GET into a class, may with a different name?
Henshal B
@henshalb
Just like an action decorator for django
John Chittum
@j5awry
hey all, we're cutting 0.4.0 release right now! flask and werkzeug both released 2.0.0. unfortunately, there are a few breaking changes we'll need to work through. You can pin flask and werkzeug yourself, but we're also pinning flask-restx for the moment till some upstream things fix, and we can make sure we've got any breaking changes accounted for
there are also some improvements and bugfixes. checkout the CHANGELOG.rst for quick hits
John Chittum
@j5awry
so i dropped by https://pypistats.org/packages/flask-restx thinking it'd show some interesting info -- our largest percentage of install is on 3.4 according to this