Sunday, July 3, 2022

#1 2021-12-31 08:17:33 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 958

NSError with userInfo properties

In most cases the Error message in ASObjC with localizedDescription() is enough to
know what is wrong...

userInfo is a instance property
https://developer.apple.com/documentati … guage=objc

In this approach I test with userInfo() and got a interesting result. The example is very basic to
make NSError and see if I could output more user friendly result.

Applescript:

use framework "Foundation"

set theURL to current application's |NSURL|'s URLWithString:"https://www.apple1.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)

-- if theData is missing value then error (theError's localizedDescription()) as text
-- error "The file couldn’t be opened." number -2700

-- if theData is missing value then error (theError's localizedRecoverySuggestion()) as text
-- error "missing value" number -2700

-- if theData is missing value then error (theError's code()) as integer
-- error "256" number -2700

-- if theData is missing value then error (theError's domain()) as text
-- error "NSCocoaErrorDomain" number -2700

if theData is missing value then error (theError's userInfo())'s |description|() as text
(**
* error "{
* NSURL = \"https://www.apple1.com\";
* }" number -2700
*)

log theError

To make it more useful why not this approach. It tell a file couldn't be open but also tell which one

Applescript:

if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}
(**
* error "The file couldn’t be opened., {
* NSURL = \"https://www.apple1.com\";
* }" number -2700
*)

return theError

To test this approach with a more useful example. The code have 2 error and properly are simple
to find. But harder if the code contains 1000 lines of code. The first run will tell you that third NSURL have a error. Next correct it... and run the script second time and the last NSURL will tell you its have a error. I have not used userInfo() before in my code when I do debugging but its a interesting approach to give little more info.

Applescript:

use framework "Foundation"

set theURL to current application's |NSURL|'s URLWithString:"https://www.apple.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

set theURL to current application's |NSURL|'s URLWithString:"https://www.google.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

set theURL to current application's |NSURL|'s URLWithString:"https://www.macscripter1.net"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

set theURL to current application's |NSURL|'s URLWithString:"https://www.bmw.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

set theURL to current application's |NSURL|'s URLWithString:"https://www.siemens.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

set theURL to current application's |NSURL|'s URLWithString:"https://www.abb1.com"
set {theData, theError} to current application's |NSData|'s alloc()'s initWithContentsOfURL:theURL options:0 |error|:(reference)
if theData is missing value then error {(theError's localizedDescription()) as text, (theError's userInfo())'s |description|() as text}

Last edited by Fredrik71 (2022-01-01 04:33:03 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)