Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.7.0+

Handle QR Code Errors

See more QR Code Examples

Demonstrates what happens when a request is refused. QrCode returns false and the response holds errors[0] with three members: a code that does not change between versions, the path of the request member at fault (a JSON Pointer such as /encoding/qr/version), and a message. Requests are checked strictly, so a misspelled option is an error rather than being silently ignored. LastErrorText has the same information in more detail.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFormats.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

    fmt.i = CkFormats::ckCreate()
    If fmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  1. A misspelled option: "moduleSise" for "moduleSize".
    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateInt(json,"render.moduleSise",8)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug "code: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].code")
        Debug "path: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].path")
        Debug "message: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].message")
    EndIf

    ;  2. A value out of range: QR versions are 1 to 40.
    CkJsonObject::ckClear(json)
    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateInt(json,"encoding.qr.version",41)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug "code: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].code")
        Debug "path: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].path")
        Debug "message: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].message")
    EndIf

    ;  3. Too much data: this text does not fit a version 1 symbol.
    CkJsonObject::ckClear(json)
    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/barcode_overview.asp")
    CkJsonObject::ckUpdateInt(json,"encoding.qr.version",1)
    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug "code: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].code")
        Debug "path: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].path")
        Debug "message: " + CkJsonObject::ckStringOf(jsonOut,"errors[0].message")
    EndIf

    ;  A program can act on the code.
    If CkJsonObject::ckStringOf(jsonOut,"errors[0].code") = "capacityExceeded"
        Debug "The text is too long for the symbol size allowed."
    EndIf

    ;  Sample output:

    ;  code: unknownOption
    ;  path: /render/moduleSise
    ;  message: unknown option
    ;  code: outOfRange
    ;  path: /encoding/qr/version
    ;  message: the value is out of range
    ;  code: capacityExceeded
    ;  path: /payload
    ;  message: the payload does not fit the requested QR version at the requested error-correction level
    ;  The text is too long for the symbol size allowed.


    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure