Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jun 30 16:25
    Bxmnx opened #451
  • Jun 30 13:23
    mh4x0f commented #160
  • Jun 30 12:59
    dhanababum commented #360
  • Jun 29 08:22
    lvhao54 commented #421
  • Jun 29 06:58
    TreeKat71 opened #450
  • Jun 27 13:25
    ikornaselur commented #263
  • Jun 21 14:52
    MajorDallas commented #59
  • Jun 21 14:50
    MajorDallas commented #59
  • Jun 21 14:31
    deamos commented #422
  • Jun 15 02:52
    hnakamitsu commented #449
  • Jun 15 02:39
    hnakamitsu labeled #449
  • Jun 15 02:39
    hnakamitsu opened #449
  • Jun 15 01:26
    nickshanks347 edited #448
  • Jun 15 01:22
    nickshanks347 opened #448
  • Jun 15 01:22
    nickshanks347 labeled #448
  • Jun 14 19:35
    thedumbterminal commented #441
  • Jun 14 12:52
    radiantone edited #447
  • Jun 14 12:39
    radiantone labeled #447
  • Jun 14 12:39
    radiantone opened #447
  • Jun 14 05:57
    ntatsumi edited #446
Brock Palen
@brockpalen
This is driving me nuts why I can't get this to work, I have an attribute that is a list of tuples [ (a, b), (a2, b2)] when I pass this attribute into fields.List() but it's being passed to the Nested model, like it's [a, b, a2, b2] . The list of tuples is from sqlalehmey, if that maters,
Brock Palen
@brockpalen
yeah this is offically 'weird' but taking the sqlalshemy output and manually building an array worked, very odd:
for entry in hardware.all():  
              hw.append({"hostname": entry.hostname, "service": entry.service, "cluster": entry.service})
While passing hardware.all() to api.model() would treat it as one big array rather than and array of sqlalchmey results,
John Chittum
@j5awry
@brockpalen : I'd have to see some more specific code. Sorry, I'm not really following what's happening
E-Kalla
@E-Kalla
Hello , I am a student and I am working on a project using FlaskRestx and i'm searching a way to catch NOHTTP error without using of 'try''catch'.
I found @api.errorhandler but It works only with http error .
I want to catch error from business function and return httpexception depending of the error catched (by mappinp BusinessError - Httpexception)
do you have any idea how can I do it ?
Thank you
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